Javascript Articles

Page 456 of 534

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Let's say, we have an array of objects like this − const arr = [{ country: "canada", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, ...

Read More

Convert 2D array to object using map or reduce in JavaScript

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

Converting a 2D array to an object is a common task in JavaScript. Let's say we have a two-dimensional array that contains data about people's ages. The data is given by the following 2D array: const data = [ ['Rahul', 23], ['Vikky', 27], ['Sanjay', 29], ['Jay', 19], ['Dinesh', 21], ['Sandeep', 45], ['Umesh', 32], ['Rohit', 28], ]; console.log(data); [ [ 'Rahul', 23 ], [ 'Vikky', 27 ], [ 'Sanjay', 29 ], ...

Read More

Convert array of arrays to array of objects grouped together JavaScript

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

Let's say, we have a two-dimensional array that contains data about some colors and fruits like this: const data = [ ['orange', 'fruit'], ['red', 'color'], ['green', 'color'], ['orange', 'color'], ['banana', 'fruit'], ['blue', 'color'], ['lemon', 'fruit'], ['mango', 'fruit'], ['lemon', 'color'], ]; console.log("Original data:", data); Original data: [ [ 'orange', 'fruit' ], [ 'red', 'color' ], [ 'green', 'color' ], [ 'orange', 'color' ], [ 'banana', 'fruit' ], ...

Read More

Intersection of two arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 722 Views

Finding the intersection of two arrays means finding elements that appear in both arrays, preserving the frequency of each element as it appears in both arrays. For example, if we have arr1 = [1, 2, 3, 1] and arr2 = [1, 3, 1], the intersection is [1, 3, 1] because element 1 appears twice in both arrays, and element 3 appears once in both. Problem Statement Given two arrays of numbers, we need to write a function that computes their intersection and returns an array containing the intersecting elements. Each element in the result should appear as ...

Read More

Find the middle element of an array using recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. So, let's write the code for this function. As you've already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be — How It ...

Read More

Group values in array by two properties JavaScript

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

We have an array of objects like this: const arr = [ { value: 12, gap: 1 }, { value: 13, gap: 1 }, { value: 14, gap: 1 }, { value: 15, gap: 1 }, { value: 19, gap: 2 }, { value: 21, gap: 1 }, { value: 22, gap: 1 }, { value: 23, gap: 1 }, { value: ...

Read More

How to count the number of elements in an array below/above a given number (JavaScript)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

In JavaScript, we often need to analyze arrays and count elements based on certain conditions. This article shows how to count array elements that fall below or above a given threshold number. Consider we have an array of numbers like this: const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]; We need to write a function that counts how many elements are below and above a given number. For example, if the number is 5.25, there should be 5 elements below it: (3.1, 1, 2.2, 5.1, 2.1) And ...

Read More

Rearrange an array in maximum minimum form by JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on. For example − // if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4] So, let's write the complete code for this function − Approach: Using Two Pointers ...

Read More

How to check if every property on object is the same recursively in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

When working with nested objects in JavaScript, you might need to check if all leaf values (final non-object values) are identical. This requires a recursive approach to traverse through all nested levels and compare the actual values at the end of each branch. For example, in this object: const obj = { a: 1, b: 1, c: { aa: 1 } }; The function should return true because all leaf values equal 1, even though one is ...

Read More

Parse array to equal intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

Let's say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals. For example: // if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48]; This way the array ...

Read More
Showing 4551–4560 of 5,340 articles
« Prev 1 454 455 456 457 458 534 Next »
Advertisements