Javascript Articles

Page 134 of 534

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 170 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 187 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 392 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

Most efficient method to groupby on an array of objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Grouping arrays of objects is a common task in JavaScript data processing. We'll explore efficient methods to group objects by single or multiple properties and aggregate values. Sample Data Let's start with an array of objects representing project phases, steps, and tasks: const arr = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" ...

Read More

Get the smallest array from an array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 545 Views

When working with nested arrays in JavaScript, you might need to find the smallest subarray based on the number of elements. This is useful for data processing, filtering operations, or when you need to identify the shortest path or sequence. Suppose we have a nested array of arrays like this: const arr = [ ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"], ["RIGHT", "LEFT", "TOP"], ["TOP", "LEFT"] ]; console.log("Original array:", arr); Original array: [ [ 'LEFT', 'RIGHT', 'RIGHT', 'BOTTOM', 'TOP' ], ...

Read More

Sorting an array by price in JavaScript

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

Sorting an array of objects by price is a common requirement in JavaScript applications. This article demonstrates how to sort house data by price values stored as strings. Sample Data Let's start with an array of house objects where prices are stored as strings: const arr = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", ...

Read More

Sort by index of an array in JavaScript

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

Sorting an array of objects by a specific property is a common task in JavaScript. This tutorial demonstrates how to sort objects by their index property and extract specific values. Problem Statement Suppose we have the following array of objects: const arr = [ { 'name': 'd', 'index': 3 }, { 'name': 'c', ...

Read More

Turning a 2D array into a sparse array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

Suppose, we have a 2-D array like this − const arr = [ [3, 1], [2, 12], [3, 3] ]; We are required to write a JavaScript function that takes in one such array. The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array. Therefore, for the input array: output[3][1] = 1; output[2][12] = 1; output[3][3] = 1; And rest all elements ...

Read More
Showing 1331–1340 of 5,340 articles
« Prev 1 132 133 134 135 136 534 Next »
Advertisements