Web Development Articles

Page 232 of 801

Transform data from a nested array to an object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 844 Views

Suppose, we have the following array of arrays: const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ]; We are required to write a JavaScript function that takes in one such nested array. The function should then prepare an object based on the array. The object for the above array should look like: const output = [ {dog: 'Harry', ...

Read More

Build tree array from flat array in JavaScript

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

Converting a flat array into a hierarchical tree structure is a common task when working with JSON data. This process transforms an array where each item has an id and parentId into a nested structure with parent-child relationships. Every entry in the JSON array has: id — a unique identifier parentId — the id of the parent node (which is "0" for root nodes) level — the depth level in the tree hierarchy The JSON data is already ordered, meaning each entry will ...

Read More

Convert JSON array into normal json in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 826 Views

Sometimes you receive data in a complex JSON array format with key-value pairs, but need to convert it to a simple flat object structure for easier access and manipulation. Problem Structure Consider this complex JSON array with nested key-value objects: const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": ...

Read More

Grouping an Array and Counting items creating new array based on Groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

When working with arrays of objects in JavaScript, you often need to group data by specific properties and count unique items. This article demonstrates how to group an array by region and count unique users per region. Problem Statement Suppose we have an array of objects representing user data across different regions: const arr = [ { region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, ...

Read More

Build tree array from JSON in JavaScript

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

Building a tree structure from a flat array is a common task in JavaScript. When you have hierarchical data represented as a flat array with codes indicating parent-child relationships, you can transform it into a nested tree structure. The Problem Suppose we have the following flat array where the code property indicates hierarchy through dot notation: const arr = [{ "code": "2", "name": "PENDING" }, { "code": "2.2", "name": "PENDING CHILDREN" }, { "code": "2.2.01.01", ...

Read More

Nested collection filter with JavaScript

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

Filtering nested collections in JavaScript requires searching through objects that contain arrays of other objects. Let's explore how to filter an array based on properties within nested objects. The Problem Suppose we have an array of nested objects where each object contains a legs array with carrier information: const arr = [{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 2, legs:[{ carrierName: 'SunExpress' }, ...

Read More

Create an object based on 2 others in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

In JavaScript, you often need to combine properties from multiple objects into a new one. There are several modern approaches to achieve this without modifying the original objects. Problem Statement Given two objects with properties and methods, we want to create a third object that contains all properties from both: const a = { a: 1, af: function() { console.log(this.a) }, }; const b = { b: 2, bf: function() { console.log(this.b) }, }; // Goal: Create object ...

Read More

Data manipulation with JavaScript

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

When working with data arrays, you often need to combine different datasets. This tutorial shows how to merge a months array with a cashflows array to create a complete dataset with all months represented. Problem Statement Suppose we have two arrays describing cashflow data: const months = ["jan", "feb", "mar", "apr"]; const cashflows = [ {'month':'jan', 'value':10}, {'month':'mar', 'value':20} ]; console.log("Months:", months); console.log("Cashflows:", cashflows); Months: [ 'jan', 'feb', 'mar', 'apr' ] Cashflows: [ { month: 'jan', value: 10 }, { month: 'mar', value: 20 ...

Read More

Find all disjointed intersections in a set of vertical line segments in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 200 Views

We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region. The origin of our coordinates system is the top-left corner, so y2 is always greater than y1. This is an example: const regions = [ [10, 100], [50, 120], [60, 180], [140, 220] ]; console.log(regions); [ [ 10, 100 ], [ 50, 120 ], [ 60, 180 ], [ ...

Read More

Efficient algorithm for grouping elements and counting duplicates in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

When working with arrays of objects in JavaScript, you often need to group elements based on shared properties and count duplicates. This is common in data processing tasks like analyzing user activity, inventory management, or survey results. Problem Overview Given an array of objects, we want to group them by a specific property and count how many times each group appears. For example, if we have objects with properties like coordinates or identifiers, we can group by the first property and display counts. Original data: X A B O Y X Z I Y ...

Read More
Showing 2311–2320 of 8,010 articles
« Prev 1 230 231 232 233 234 801 Next »
Advertisements