Javascript Articles

Page 450 of 534

How to sum elements at the same index in array of arrays into a single array? JavaScript

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

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] Let's explore different approaches to solve this problem. Using forEach() Method The most straightforward approach is to iterate through each sub-array and accumulate ...

Read More

Get the property of the difference between two objects in JavaScript

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

When working with JavaScript objects, you often need to compare them and identify which properties have different values. This is useful for tracking changes, validation, or debugging purposes. Problem Overview Given two objects with similar key-value pairs, we need to write a function that finds the first key with different values between the objects. If all values match, the function should return -1. Here are sample objects to demonstrate the concept: const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, ...

Read More

Merge two arrays with alternating Values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 923 Views

Merging two arrays with alternating values means creating a new array that picks elements from each array in turn: first element from array1, first element from array2, second element from array1, and so on. Using While Loop with Index Tracking This approach uses separate counters to track positions in each array and alternates between them: const arr1 = [34, 21, 2, 56, 17]; const arr2 = [12, 86, 1, 54, 28]; let run = 0, first = 0, second = 0; const newArr = []; while(run < arr1.length + arr2.length){ if(first ...

Read More

Highest and lowest in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 487 Views

In JavaScript, finding the difference between the highest and lowest values in an array is a common task. This article demonstrates multiple approaches to calculate this difference efficiently. Using Math.max() and Math.min() with Spread Operator The most straightforward approach uses the spread operator with Math.max() and Math.min(): const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454]; const arrayDifference = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); return max - min; }; console.log("Array:", arr); ...

Read More

Sorting objects according to days name JavaScript

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

Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...

Read More

Remove number properties from an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

In JavaScript, we often need to filter object properties based on their data types. This article demonstrates how to remove properties of a specific type from an object using a reusable function. Problem Statement Given an object with mixed property types (numbers, strings, booleans, objects), we need to create a function that removes all properties of a specified data type. If no type is specified, it should default to removing number properties. Solution We'll create a function called shedData that iterates through object properties and deletes those matching the specified type: const obj = ...

Read More

Check if object contains all keys in JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 Views

We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array. So, let's write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that's not a key of object, we exit and return false, otherwise we return true. Example const obj = { ...

Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 176 Views

We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...

Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 694 Views

We are given an array of numbers and a target sum. Our job is to write a function that returns an array of all the subarrays which add up to the target number using the sliding window algorithm. For example: const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; Should find these subarrays that sum to 40: [ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ] The Sliding Window Algorithm The sliding window algorithm ...

Read More

Sort array based on presence of fields in objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

In JavaScript, you can sort arrays of objects based on the presence of specific fields using a custom comparator function. This technique is useful when you need to prioritize objects with complete information. Problem Statement Let's say we have an array of people objects where some have both firstName and lastName, some have only one of these properties, and others have neither: const people = [{ firstName: 'Ram', id: 301 }, { firstName: 'Shyam', lastName: 'Singh', ...

Read More
Showing 4491–4500 of 5,340 articles
« Prev 1 448 449 450 451 452 534 Next »
Advertisements