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
Web Development Articles
Page 503 of 801
Splitting 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 MoreHow to remove blank (undefined) elements from JavaScript array - JavaScript
When working with JavaScript arrays, you may encounter sparse arrays containing empty slots (undefined elements). These gaps can occur when elements are deleted or when arrays are created with missing values. const arr = [4, 6, , 45, 3, 345, , 56, 6]; console.log(arr); console.log("Length:", arr.length); [ 4, 6, , 45, 3, 345, , 56, 6 ] Length: 9 We need to remove only the undefined and empty values, not all falsy values like 0, false, or empty strings. Method 1: Using splice() with for loop Use a for loop to ...
Read More