Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 483 of 534
How to run a function after two async functions complete - JavaScript
When working with multiple asynchronous operations in JavaScript, you often need to wait for all of them to complete before executing a final function. This is a common scenario in web development where you might be fetching data from multiple APIs or processing several files concurrently. There are several approaches to handle this challenge, with Promise.all() being the most efficient for running multiple async operations concurrently. Using Promise.all() (Recommended) Promise.all() executes multiple promises concurrently and waits for all of them to resolve. It's the optimal choice when you need all operations to complete regardless of their individual ...
Read MoreSplitting an array based on its first value - JavaScript
Suppose we have an array of arrays of numbers like this: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log(arr); [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ], [ 2, 67 ], [ 4, 65 ] ] Each subarray contains exactly two elements. We need to write a function that constructs a new array where all second elements of subarrays with the same first value are grouped together. For the ...
Read MoreSplitting strings based on multiple separators - JavaScript
We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified. For example, if the string is: const str = 'rttt.trt/trfd/trtr, tr'; And the separators are: const sep = ['/', '.', ', ']; Then the output should be: const output = ['rttt', 'trt', 'trfd', 'trtr']; Method 1: Using Manual Iteration This approach manually iterates through each character and checks if ...
Read MoreFetching JavaScript keys by their values - JavaScript
In JavaScript, you can find an object key by its value using several approaches. This is useful when you need to perform reverse lookups in objects where both keys and values are unique. Consider this object where we want to find keys by their corresponding values: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; console.log(products); { Pineapple: 38, Apple: 110, Pear: 109 } Using Object.keys() with find() The most straightforward approach uses Object.keys() with find() to locate the ...
Read MoreGroup values on same property - JavaScript
When working with arrays of objects, you often need to group items that share the same property value. This tutorial shows how to group objects by a common property and combine other properties. The Problem Suppose we have an array of objects with unit and brand properties: const arr = [ {unit: 35, brand: 'CENTURY'}, {unit: 35, brand: 'BADGER'}, {unit: 25, brand: 'CENTURY'}, {unit: 15, brand: 'CENTURY'}, {unit: 25, brand: 'XEGAR'} ]; console.log("Original array:"); console.log(arr); ...
Read MoreSum arrays repeated value - JavaScript
Suppose, we have an array of objects like this − const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ]; We are required to add the values for all these objects together that have identical keys Therefore, for this array, the output should be − const output = [{'ID-01':4}, {'ID-02':8}]; We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the ...
Read MoreConvert JS array into an object - JavaScript
Converting a JavaScript array into an object is a common task in web development. This article shows different approaches to transform an array of objects into a single object using various JavaScript methods. Suppose we have an array of objects like this: const arr = [ {id: 1, name: "Mohan"}, {id: 2, name: "Sohan"}, {id: 3, name: "Rohan"} ]; console.log("Input array:", arr); Input array: [ { id: 1, name: 'Mohan' }, { id: 2, name: 'Sohan' }, { id: 3, name: 'Rohan' ...
Read MoreFinding the sum of unique array values - JavaScript
We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. For example, if the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; Then the output should be 25 (3 + 7 + 4 + 11 = 25), as these are the only elements that appear exactly once. Using indexOf() and lastIndexOf() ...
Read MoreHow to subtract elements of two arrays and store the result as a positive array in JavaScript?
Suppose, we have two arrays like these − const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of the array. Therefore, for these arrays, the output should look like − const output = [8, 6, 4, 1, 3, 3]; We will use a for loop and keep pushing the absolute difference iteratively into a new array and ...
Read MoreSort object array based on another array of keys - JavaScript
When working with arrays of objects in JavaScript, you may need to sort one array based on the order defined in another array. This is particularly useful when you have a reference array that defines the desired sorting order. Problem Statement Suppose we have two arrays like these: const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("Original arrays:"); console.log("arr1:", arr1); console.log("arr2:", arr2); Original arrays: arr1: [ 'd', 'a', 'b', 'c' ] arr2: [ { a: 1 }, { c: 3 }, { d: 4 }, { ...
Read More