Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 128 of 589
How to check whether multiple values exist within a JavaScript array
We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Following are our arrays − const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; Let's write the code and check for multiple values − Using indexOf() Method const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => { const ...
Read MoreSorting arrays by two criteria in JavaScript
When sorting arrays by multiple criteria in JavaScript, you need to create a custom comparator function that handles each condition sequentially. This article demonstrates sorting objects by priority (isImportant) first, then by date within each priority group. The Array Structure Consider an array of objects with id, date, and isImportant properties. We want important items first, then sort by date within each group: const array = [{ id: 545, date: 591020824000, isImportant: false, }, { id: 322, ...
Read MoreAdd two consecutive elements from the original array and display the result in a new array with JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is: const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Then the output should be: const newArrayOne = [1, 5, 9, 13, 17] Let's write the code for this function: Example const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; ...
Read MoreSorting by 'next' and 'previous' properties (JS comparator function)
When working with linked data structures in JavaScript, you may encounter objects that reference each other through 'next' and 'previous' properties. This tutorial demonstrates how to sort such objects into their correct sequential order. Problem Statement Consider an array of objects representing pages of a website, where each object has: An id property for unique identification A next property pointing to the next page's id (unless it's the last page) A previous property pointing to the previous page's id (unless it's the first page) These objects are randomly positioned in the array, and we need ...
Read MoreJavaScript Remove all '+' from array wherein every element is preceded by a + sign
When working with arrays containing strings that start with a '+' sign, you can remove these characters using JavaScript's map() method combined with replace(). Problem Statement Let's say we have an array where every element is preceded by a '+' sign: var studentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ]; Using map() with replace() The map() method creates a new array by calling a function on each element. We use replace() to ...
Read MoreConvert object of objects to array in JavaScript
Let's say we have the following object of objects that contains rating of some Indian players, we need to convert this into an array of objects with each object having two properties namely name and rating where name holds the player name and rating holds the rating object. Following is our sample object: const playerRating = { 'V Kohli': { batting: 99, fielding: 99 }, 'R Sharma': { batting: 98, fielding: 95 }, ...
Read MoreExtract the value of the to a variable using JavaScript?
To extract the value of the element to a variable using JavaScript, use the textContent property to get the text content from any HTML or SVG text element. Syntax var textValue = document.getElementById("elementId").textContent; Example Extract SVG Text Value This is the JavaScript Tutorial ...
Read MoreStop making form to reload a page in JavaScript
When a form is submitted, browsers reload the page by default. To prevent this behavior and handle form submissions with JavaScript, we need to stop the default form submission event. The Problem HTML forms automatically reload the page when submitted. This interrupts JavaScript processing and user experience: Method 1: Using preventDefault() with Form Submit Event The most reliable approach is using event.preventDefault() in the form's submit event handler: ...
Read MoreHow can we separate the special characters in JavaScript?
In JavaScript, you can separate special characters from text using the match() method with regular expressions. This technique splits strings into arrays containing both word characters and special characters as separate elements. Syntax arrayName.flatMap(item => item.match(/\w+|\W+/g)); The regular expression /\w+|\W+/g works as follows: \w+ matches one or more word characters (letters, digits, underscore) | acts as OR operator \W+ matches one or more non-word characters (special characters, spaces) g flag ensures global matching throughout the string Example: Separating Special Characters from Names Let's separate special characters from an array of ...
Read MoreHow to convert array to object in JavaScript
Converting arrays to objects is a common task in JavaScript. There are several approaches depending on your data structure and requirements. Method 1: Array of Arrays to Objects with Alphabetic Keys Let's convert a nested array structure into objects with keys as English alphabet letters: const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]; const dataArr = data.map(arr => { return arr.reduce((acc, cur, index) => ({ ...acc, [String.fromCharCode(97 + index)]: ...
Read More