Javascript Articles

Page 127 of 534

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 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 225 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 826 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 195 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 132 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

Implementing Math function and return m^n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

We are required to write a JavaScript function that takes in two numbers say m and n. Then function should calculate and return m^n. For example − For m = 4, n = 3, then power(4, 3) = 4^3 = 4 * 4 * 4 = 64 power(6, 3) = 216 Using Built-in Math.pow() Method JavaScript provides the built-in Math.pow() method for calculating powers: console.log(Math.pow(4, 3)); // 4^3 console.log(Math.pow(6, 3)); // 6^3 console.log(Math.pow(2, -2)); // 2^(-2) = 1/4 64 216 0.25 Custom Implementation Using Recursion ...

Read More

Finding the sub array that has maximum sum JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 568 Views

We are required to write a JavaScript function that takes in an array of numbers. The array can contain both positive and negative numbers. The purpose of our function is to find the subarray (of any length) whose elements when summed give the maximum sum. This problem is known as the Maximum Subarray Problem and is commonly solved using Kadane's Algorithm. For example, if the input array is: const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; Then the output should be: 6 Because the subarray [4, ...

Read More

Difference between two strings JavaScript

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

We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position. We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t. For example − If the input strings are − const s = "abcd", t = "abcde"; Then the output should be − const output = "e"; because 'e' is the letter that was added. Method 1: Using XOR ...

Read More

Checking for overlapping times JavaScript

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

Time interval overlap detection is a common programming challenge. We need to determine if any two time intervals in an array share common time periods. Problem Definition Given an array of time intervals with start and end times, we need to check if any two intervals overlap. Two intervals overlap when they have some time in common. const arr = [ { start: '01:00', end: '04:00' }, { start: '05:00', end: '08:00' }, { start: '07:00', end: '11:00' }, { start: '09:30', end: '18:00' ...

Read More

Sorting an array of objects by an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 687 Views

In JavaScript, you can sort an array of objects based on the order defined by another array. This is useful when you need to prioritize objects according to a specific sequence. Problem Statement Given an array of objects and a reference array, we need to sort the objects according to the order specified in the reference array. const orders = [ { status: "pending"}, { status: "received" }, { status: "sent" }, { status: "pending" } ]; const statuses = ...

Read More
Showing 1261–1270 of 5,340 articles
« Prev 1 125 126 127 128 129 534 Next »
Advertisements