Web Development Articles

Page 225 of 801

Partition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We need to write a JavaScript function that partitions a number into chunks following specific rules: The number of chunks must be a power of 2 (1, 2, 4, 8, 16, etc.) Each chunk size must also be a power of 2, with a maximum limit Understanding the Problem Let's examine how different numbers can be partitioned: For number 8: [8] This works because we have 1 chunk (power of 2) with size 8 (power of 2). For number 9: [8, 1] This works because we have 2 chunks (power of 2), each ...

Read More

Flat array of objects to tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 834 Views

Converting a flat array of objects into a tree structure is a common requirement in web development. This involves organizing objects with parent-child relationships into a hierarchical format. The Data Structure We start with a flat array where each object has an id, name, and parentId. Objects with parentId: null are root nodes, while others are children. .parent, .child { cursor: pointer; ...

Read More

JavaScript Bubble sort for objects in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 614 Views

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list and swapping adjacent elements if they are in the wrong order. When applied to an array of objects, we can sort based on specific properties. The Shoe Class Let's start with a constructor class that creates Shoe objects: class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = ...

Read More

Converting a comma separated string to separate arrays within an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 672 Views

When working with comma-separated strings containing hierarchical data, we often need to parse them into structured objects. This article shows how to convert a string like 'dress/cotton/black, dress/leather/red' into an organized object with arrays. The Problem Suppose we have a string like this: const str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james'; We need to create a JavaScript function that converts this into an object where each category becomes a key, and all related values are grouped into arrays: const output = { dress: ["cotton", "black", "leather", "red", "fabric"], ...

Read More

Count number of entries in an object having specific values in multiple keys JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

In JavaScript, you often need to count objects in an array that match specific criteria across multiple properties. This is commonly done using the filter() method combined with conditional logic. Suppose we have an array of objects representing trade data: const arr = [ {"goods":"Wheat", "from":"GHANA", "to":"AUSTRALIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"}, {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ]; We need to count objects where the "from" property equals "USA" and the "to" property equals "INDIA". Using filter() and length ...

Read More

How to sort a JavaScript object list based on a property when the property is not consistent

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

When sorting JavaScript object arrays with inconsistent properties, you need a custom comparator function. This scenario commonly occurs when some objects have date values while others have null or undefined dates. The requirement is to display objects without dates at the top (sorted alphabetically by name), followed by objects with dates (sorted chronologically). Problem Overview Consider an array where some objects have date properties and others don't: const items = [ { name: "Charlie", date: "2024-03-15" }, { name: "Alice", date: null }, ...

Read More

Convert 2d tabular data entries into an array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

Converting 2D tabular data into an array of objects is a common requirement when working with datasets. This transformation groups rows by a unique identifier and creates objects with dynamic properties. Problem Overview Suppose we have an array of arrays representing tabular data: const arr = [ ["Ashley", "2017-01-10", 80], ["Ashley", "2017-02-10", 75], ["Ashley", "2017-03-10", 85], ["Clara", "2017-01-10", 90], ["Clara", "2017-02-10", 82] ]; We need to transform this into an array of objects where each unique name becomes a ...

Read More

Best way to flatten an object with array properties into one array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 855 Views

When you have an object containing arrays as values, you often need to flatten all these arrays into a single array. This is a common operation when working with grouped data. const obj = { arr_a: [9, 3, 2], arr_b: [1, 5, 0], arr_c: [7, 18] }; console.log("Original object:", obj); Original object: { arr_a: [ 9, 3, 2 ], arr_b: [ 1, 5, 0 ], arr_c: [ 7, 18 ] } The goal is to merge all array values into one ...

Read More

Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

Subtracting arrays in JavaScript involves removing all elements from the first array that exist in the second array. This operation is useful for filtering data and finding differences between datasets. Problem Statement Given two arrays, we need to delete all elements from the first array that are also present in the second array. const arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro']; // Expected output: ['uno', 'tres'] Using filter() with indexOf() The filter() method creates a new array with elements that pass a test. We use indexOf() to check ...

Read More

JavaScript: compare array element properties, and if identical, combine

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 135 Views

When working with arrays of objects, you often need to combine objects that have identical properties. In this example, we'll consolidate storage devices with the same size by adding up their counts. Suppose we have an array of objects containing information about data storage devices: const drives = [ {size:"900GB", count:3}, {size:"900GB", count:100}, {size:"1200GB", count:5}, {size:"900GB", count:1} ]; console.log("Original array:", drives); Original array: [ { size: '900GB', count: 3 }, { size: '900GB', count: ...

Read More
Showing 2241–2250 of 8,010 articles
« Prev 1 223 224 225 226 227 801 Next »
Advertisements