Front End Technology Articles

Page 227 of 652

JavaScript - Merge two arrays according to id property

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

When working with JavaScript arrays of objects, you often need to merge two arrays based on a common property like an ID. This is useful for combining related data from different sources, such as user profiles and addresses. Suppose we have two arrays of objects. The first contains user IDs and names, while the second contains user IDs and addresses: const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ...

Read More

Trim and split string to form array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 919 Views

When working with comma-separated strings that contain unwanted whitespace, you need to trim spaces and split the string into an array. JavaScript provides multiple approaches to accomplish this task efficiently. Problem Statement Given a comma-separated string with irregular spacing: const str = "a, b, c, d , e"; console.log("Original string:", str); Original string: a, b, c, d , e We need to remove all whitespaces and split it into a clean array of elements. Method 1: Manual Space Removal This approach manually loops through the string to remove spaces ...

Read More

Array of objects to array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 960 Views

Converting an array of objects to an array of arrays is a common transformation in JavaScript. This is useful when you need to extract object values into a simpler array format for data processing or visualization. Problem Statement Suppose we have an array of objects like this: const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ]; We need to transform this into an array of arrays containing the object values: [ ...

Read More

Form a sequence out of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 867 Views

When working with sorted arrays containing consecutive numbers, we often need to represent sequences in a compressed format. Instead of listing all consecutive numbers, we can show ranges using a dash (-) separator. For example, instead of [1, 2, 3, 5, 7, 8, 9, 11], we want to display "1-3, 5, 7-9, 11". Problem Statement Given a sorted array of numbers, we need to: Group consecutive numbers into ranges (e.g., 1, 2, 3 becomes "1-3") Keep standalone numbers as-is Join all parts with commas const arr = [1, 2, 3, 5, 7, 8, ...

Read More

Join two objects by key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 900 Views

When working with related data structures, you often need to join objects from different arrays based on matching keys. This is common when dealing with parent-child relationships or normalized data. Suppose we have two arrays - one containing child objects and another containing parent objects: const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child ...

Read More

Retrieve property value selectively from array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

When working with arrays of objects in JavaScript, you often need to extract specific property values based on certain conditions. This tutorial shows how to retrieve property values selectively from an array of objects. Suppose we have an array of objects like this: const arr = [ { id : "23", name : "Item 1", isActive : true}, { id : "25", name : "Item 2", isActive : false}, { id : "26", name : "Item 3", isActive : false}, { id : "30", name ...

Read More

Join in nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

Joining elements from nested arrays in JavaScript requires flattening the array structure first. This article explores different approaches to join nested array elements with a semicolon separator. The Problem Consider this nested array: const arr = ['zero', ['one', 'two', 'three', ['four', ['five', 'six', ['seven']]]]]; console.log("Original nested array:", arr); Original nested array: [ 'zero', [ 'one', 'two', 'three', [ 'four', [Array] ] ] ] We need to extract all elements and join them with semicolons to get: zero;one;two;three;four;five;six;seven; Using flat() Method (Modern Approach) The flat(Infinity) method flattens ...

Read More

Remove item from a nested array by indices in JavaScript

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

Suppose we have a nested array of objects like this: const arr = [ { value: 'some value' }, { array: [ { value: 'some value' }, { array: [ ...

Read More

Combining two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

In JavaScript, combining two arrays means pairing corresponding elements from each array to create a new array of subarrays. This is commonly known as "zipping" arrays together. For example, if we have two arrays of the same length, we can combine their elements at matching indices to form pairs. If the two arrays are: const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; Then the expected output should be: [ ['a', 1], ['b', 2], ['c', 3] ] ...

Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...

Read More
Showing 2261–2270 of 6,519 articles
« Prev 1 225 226 227 228 229 652 Next »
Advertisements