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 130 of 534
Array of objects to array of arrays in JavaScript
Converting an array of objects to an array of arrays is a common transformation in JavaScript. This is useful when you need to extract object values into a simpler array format for data processing or visualization. Problem Statement Suppose we have an array of objects like this: const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ]; We need to transform this into an array of arrays containing the object values: [ ...
Read MoreForm a sequence out of an array in JavaScript
When working with sorted arrays containing consecutive numbers, we often need to represent sequences in a compressed format. Instead of listing all consecutive numbers, we can show ranges using a dash (-) separator. For example, instead of [1, 2, 3, 5, 7, 8, 9, 11], we want to display "1-3, 5, 7-9, 11". Problem Statement Given a sorted array of numbers, we need to: Group consecutive numbers into ranges (e.g., 1, 2, 3 becomes "1-3") Keep standalone numbers as-is Join all parts with commas const arr = [1, 2, 3, 5, 7, 8, ...
Read MoreJoin two objects by key in JavaScript
When working with related data structures, you often need to join objects from different arrays based on matching keys. This is common when dealing with parent-child relationships or normalized data. Suppose we have two arrays - one containing child objects and another containing parent objects: const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child ...
Read MoreRetrieve property value selectively from array of objects in JavaScript
When working with arrays of objects in JavaScript, you often need to extract specific property values based on certain conditions. This tutorial shows how to retrieve property values selectively from an array of objects. Suppose we have an array of objects like this: const arr = [ { id : "23", name : "Item 1", isActive : true}, { id : "25", name : "Item 2", isActive : false}, { id : "26", name : "Item 3", isActive : false}, { id : "30", name ...
Read MoreJoin in nested array in JavaScript
Joining elements from nested arrays in JavaScript requires flattening the array structure first. This article explores different approaches to join nested array elements with a semicolon separator. The Problem Consider this nested array: const arr = ['zero', ['one', 'two', 'three', ['four', ['five', 'six', ['seven']]]]]; console.log("Original nested array:", arr); Original nested array: [ 'zero', [ 'one', 'two', 'three', [ 'four', [Array] ] ] ] We need to extract all elements and join them with semicolons to get: zero;one;two;three;four;five;six;seven; Using flat() Method (Modern Approach) The flat(Infinity) method flattens ...
Read MoreRemove item from a nested array by indices in JavaScript
Suppose we have a nested array of objects like this: const arr = [ { value: 'some value' }, { array: [ { value: 'some value' }, { array: [ ...
Read MoreCombining two arrays in JavaScript
In JavaScript, combining two arrays means pairing corresponding elements from each array to create a new array of subarrays. This is commonly known as "zipping" arrays together. For example, if we have two arrays of the same length, we can combine their elements at matching indices to form pairs. If the two arrays are: const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; Then the expected output should be: [ ['a', 1], ['b', 2], ['c', 3] ] ...
Read MoreJavaScript: create an array of JSON objects from linking two arrays
Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...
Read MoreRemove the duplicate value from array with images data in JavaScript
When working with arrays of objects containing image data, you often need to remove duplicates based on a specific property. Here's how to remove duplicate objects based on the 'image' property value. Sample Data Consider an array of image objects where some images appear multiple times: const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { ...
Read MoreFilter JavaScript array of objects with another array
Suppose, we have an array of objects like this − const arr = [ {area: 'NY', name: 'Bla', ads: true}, {area: 'DF', name: 'SFS', ads: false}, {area: 'TT', name: 'SDSD', ads: true}, {area: 'SD', name: 'Engine', ads: false}, {area: 'NSK', name: 'Toyota', ads: false}, ]; We are required to write a JavaScript function that takes in one such array as the first argument and an array of string literals as the second argument. Our function should then filter the input ...
Read More