Object Oriented Programming Articles

Page 128 of 589

How to check whether multiple values exist within a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Sorting arrays by two criteria in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

Add two consecutive elements from the original array and display the result in a new array with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

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 More

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

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 More

JavaScript Remove all '+' from array wherein every element is preceded by a + sign

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

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 More

Convert object of objects to array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

Extract the value of the to a variable using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 798 Views

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 More

Stop making form to reload a page in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 8K+ Views

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 More

How can we separate the special characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 368 Views

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 More

How to convert array to object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

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
Showing 1271–1280 of 5,881 articles
« Prev 1 126 127 128 129 130 589 Next »
Advertisements