Object Oriented Programming Articles

Page 145 of 589

Reorder array based on condition in JavaScript?

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

Let's say we have an array of objects that contains the scores of some players in a card game: const scorecard = [{ name: "Zahir", score: 23 }, { name: "Kabir", score: 13 }, { name: "Kunal", score: 29 }, { name: "Arnav", score: 42 }, { name: "Harman", score: 19 }, { name: ...

Read More

How to match strings that aren't entirely digits in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 115 Views

In JavaScript, you can use regular expressions to match strings that contain non-digit characters. This is useful when parsing complex data strings where you need to identify values that aren't purely numeric. Problem Statement Consider this complex string containing various data types: studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678" We want to extract values that contain characters other than digits, excluding boolean values and null. Using Regular Expression The following regular expression matches strings that aren't entirely digits: var regularExpression = /(?

Read More

Count unique elements in array without sorting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 408 Views

When working with arrays containing duplicate values, counting unique elements is a common task. Let's explore different approaches to count unique elements in an array without sorting it first. Consider this sample array with duplicate values: const arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion']; console.log('Original array:', arr); Original array: ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion'] Using Array.reduce() and lastIndexOf() This approach uses lastIndexOf() to identify the last occurrence of each element, ensuring each unique value is counted only once: const ...

Read More

How to combine two arrays into an array of objects in JavaScript?

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

Combining two arrays into an array of objects is a common task in JavaScript. This allows you to pair corresponding elements from both arrays into structured data. Using map() with Object Creation The map() method creates a new array by transforming each element. We can use it to combine arrays into objects: var firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol']; var arrayOfObjects = firstArray.map(function(value, index) { return { first: value, second: ...

Read More

Top n max value from array of object JavaScript

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

Finding the top n objects with maximum values from an array is a common programming task. Let's explore how to get objects with the highest duration values from an array of objects. Problem Statement Given an array of objects, we need to create a function that returns the top n objects based on the highest duration values. const arr = [ {"id":0, "start":0, "duration":117, "slide":4, "view":0}, {"id":0, "start":0, "duration":12, "slide":1, "view":0}, {"id":0, "start":0, "duration":41, "slide":2, "view":0}, {"id":0, "start":0, "duration":29, "slide":3, ...

Read More

How to convert an array into a complex array JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

In JavaScript, converting a flat array into a complex nested array structure involves grouping elements based on specific conditions. A common use case is splitting an array into subarrays when the sum of consecutive elements exceeds a given threshold. Let's say we need to write a function that takes an array of numbers and a number n, where n is the maximum sum allowed for each subarray. The function should break the array into subarrays whenever the sum of consecutive elements would exceed n. Example Problem Given an array and a threshold value, we want to create ...

Read More

Can JavaScript parent and child classes have a method with the same name?

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

Yes, JavaScript parent and child classes can have methods with the same name. This concept is called method overriding, where the child class provides its own implementation of a method that already exists in the parent class. Method Overriding Example class Parent { constructor(parentValue) { this.parentValue = parentValue; } // Parent class method showValues() { console.log("The parent method is called....."); ...

Read More

How to generate child keys by parent keys in array JavaScript?

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

Let's say, we have an array of objects representing a hierarchical structure where each object has an id, parent_id, and title: const arr = [ { id: 1, parent_id: 0, title: 'Movies' }, { id: 2, parent_id: 0, title: 'Music' }, { id: 3, parent_id: 1, title: 'Russian movies' }, { id: 4, parent_id: 2, title: 'Russian music' }, { id: 5, parent_id: 3, title: 'New' }, { id: 6, parent_id: 3, title: 'Top10' ...

Read More

Compare two arrays and get those values that did not match JavaScript

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

We have two arrays of literals that contain some common values, our job is to write a function that returns an array with all those elements from both arrays that are not common. For example − // if the two arrays are: const first = ['cat', 'dog', 'mouse']; const second = ['zebra', 'tiger', 'dog', 'mouse']; // then the output should be: const output = ['cat', 'zebra', 'tiger'] // because these three are the only elements that are not common to both arrays Let's write the code for this − We will spread the two ...

Read More

Remove values in an array by comparing the items 0th index in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

When working with arrays of sub-arrays, you may need to remove duplicates based on the first element (0th index) of each sub-array. This is common when dealing with data like subject-marks pairs where you want only unique subjects. Let's say the following is our array: var subjectNameAlongWithMarks = [ ["JavaScript", 78], ["Java", 56], ["JavaScript", 58], ["MySQL", 77], ["MongoDB", 75], ["Java", 98] ]; console.log("Original array:", subjectNameAlongWithMarks); Original array: [ ...

Read More
Showing 1441–1450 of 5,881 articles
« Prev 1 143 144 145 146 147 589 Next »
Advertisements