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
Javascript Articles
Page 458 of 534
Aggregate records in JavaScript
Aggregating records in JavaScript involves combining multiple objects with the same identifier into a single object with accumulated values. This is commonly used for data analysis tasks like summing transactions by person or grouping sales by product. Let's say we have an array of objects that contains information about some random transactions carried out by some people: const transactions = [{ name: 'Rakesh', amount: 1500 }, { name: 'Rajesh', amount: 1200 }, { name: 'Ramesh', ...
Read MoreReorder array based on condition in JavaScript?
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 MoreCount unique elements in array without sorting JavaScript
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 MoreTop n max value from array of object JavaScript
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 MoreHow to convert an array into a complex array JavaScript?
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 MoreHow to generate child keys by parent keys in array JavaScript?
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 MoreCompare two arrays and get those values that did not match JavaScript
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 MoreShift last given number of elements to front of array JavaScript
In JavaScript, moving elements from the end of an array to the front is a common array manipulation task. This tutorial shows how to create a function that shifts the last n elements to the beginning of an array in-place. Problem Statement We need to create an array method reshuffle() that: Takes a number n (where n ≤ array length) Moves the last n elements to the front Modifies the array in-place Returns true on success, false if n exceeds array length Example Input and Output // Original array const arr = ["blue", ...
Read MoreArray sum: Comparing recursion vs for loop vs ES6 methods in JavaScript
When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce(). To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks. Recursive Approach The recursive method calls itself until it processes all array elements: const recursiveSum = (arr, len = 0, sum = 0) => { if(len < arr.length){ ...
Read MoreCompare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity
When comparing objects in JavaScript, we often need to determine their similarity as a percentage. This is useful for data matching, filtering, or recommendation systems. Problem Overview Given two objects, we need to calculate their similarity percentage based on matching key-value pairs. The similarity is calculated by dividing the count of matching properties by the total properties in the smaller object. const a = { Make: "Apple", Model: "iPad", hasScreen: "yes", Review: "Great product!", }; const b = { ...
Read More