Front End Technology Articles

Page 217 of 652

How to find inside an array of objects the object that holds the highest value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 846 Views

Finding the object with the highest value in an array of objects is a common task in JavaScript. This example demonstrates how to find the student with the highest grade from an array of student objects. Sample Data Let's start with an array of student objects, where each student has a name and an array of grades: const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] ...

Read More

How to merge two object arrays of different size by key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 879 Views

When working with object arrays of different sizes, we often need to merge them based on a common key. This is useful when combining data from different sources that share a common identifier. Suppose we have an object like this: const obj = { "part1": [{"id": 1, "a": 50}, {"id": 2, "a": 55}, {"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] }; We need to merge part1 and part2 arrays to form a single array where objects with the same id ...

Read More

Deep Search JSON Object JavaScript

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

Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...

Read More

Convert JSON to another JSON format with recursion JavaScript

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

Suppose, we have the following JSON object − const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", ...

Read More

Sorting Array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

Sorting an array based on another array's order is a common JavaScript task. This technique allows you to reorder elements according to a reference array's sequence. Problem Statement Given two arrays, we need to sort the first array based on the order of elements in the second array: const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; console.log("Original array:", input); console.log("Reference order:", sortingArray); Original array: [ 'S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8' ] Reference order: [ 'S-1', ...

Read More

How to find and return the longest repeating series of numbers in array with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 649 Views

We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array. For example − If the input array is − const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number). Using Array.reduce() Method The reduce() method can be used to group ...

Read More

JavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

We need to write a JavaScript function that takes a multidimensional array (array of arrays) as the first argument and a single array as the second argument. The function should find common elements between each subarray and the single array, returning a new array containing only the matching elements from each subarray. Problem Understanding For each subarray in the multidimensional array, we want to find elements that also exist in the single array. The result preserves the structure but only includes common elements. For example, if we have: const arr1 = [ ...

Read More

JavaScript group array - find the sets of numbers that can be traveled to using the edges defined

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 155 Views

Consider the following input and output arrays: const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ]; Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined. In graph theory terms, we need to find the distinct connected components within such a graph. For instance, in the above arrays, there ...

Read More

Reversing vowels in a string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 448 Views

We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string. Problem Example If the input string is: const str = 'Hello'; Then the output should be: 'Holle' Notice how 'e' and 'o' swap positions while consonants 'H', 'l', 'l' remain in their original places. Solution Using Two Pointers The most efficient approach uses two pointers from opposite ends of the string: const str = 'Hello'; const reverseVowels = (str = '') => { ...

Read More

Finding square root of a non-negative number without using Math.sqrt() JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 434 Views

We are required to write a JavaScript function that takes in a non-negative integer and computes and returns its square root without using Math.sqrt(). We can floor off a floating-point number to an integer. For example: For the number 15, we need not return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15. We will make use of the binary search algorithm to converge to the square root of the given number. Binary Search Approach The binary search method works by maintaining a range [left, ...

Read More
Showing 2161–2170 of 6,519 articles
« Prev 1 215 216 217 218 219 652 Next »
Advertisements