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 129 of 534
Search from an array of objects via array of string to get array of objects in JavaScript
Filtering an array of objects based on keys from another array is a common requirement in JavaScript. This tutorial shows how to extract matching objects using various approaches. Problem Statement Given an array of strings and an array of objects, we need to filter objects whose KEY property exists in the string array: const searchKeys = ['1956888670', '2109171907', '298845084']; const users = [ { KEY: '1262875245', VALUE: 'Vijay Kumar Verma' }, { KEY: '1956888670', VALUE: 'Sivakesava Nallam' }, { KEY: '2109171907', VALUE: 'udm analyst' ...
Read MoreConvert array with duplicate values to object with number of repeated occurrences in JavaScript
When working with arrays that contain duplicate values, you often need to count occurrences and convert them into a structured format. This article shows how to transform an array with duplicates into an array of objects containing each unique value and its count. Problem Statement Suppose we have an array with duplicate entries like this: const arr = ['California', 'Texas', 'Texas', 'Texas', 'New York', 'Missouri', 'New Mexico', 'California']; console.log("Original array:", arr); Original array: [ 'California', 'Texas', 'Texas', 'Texas', 'New York', ...
Read MoreFilter nested object by keys using JavaScript
When working with arrays of objects in JavaScript, you often need to filter them based on specific criteria. This article demonstrates how to filter an array of objects by checking if their title property contains any of the specified search terms. Problem Statement Suppose we have an array of objects like this: const arr = [{ 'title': 'Hey', 'foo': 2, 'bar': 3 }, { 'title': 'Sup', 'foo': 3, 'bar': 4 }, { ...
Read MoreJavaScript - Determine all possible ways a group of values can be removed from a sequence
We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance of each value from the original sequence. For example − If the sequence array is − const arr = [1, 2, 1, 3, 1, 4, 4]; And the array to be removed is − const arr2 = [1, 4, 4]; Then there are three possible ways of doing this without disrupting the ...
Read MoreSort array by month-year JavaScript
Suppose, we have an array that contains dates in MM-YYYY format like this − const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; console.log("Original array:", arr); Original array: [ '1-2016', '7-2015', '7-2016', '3-2016', '8-2016', '2-2016', '6-2016', '8-2015', '5-2016', '4-2016', '9-2015', '10-2015', '11-2015', '12-2015' ] We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the ...
Read MoreHow to dynamically combine all provided arrays using JavaScript?
When working with arrays in JavaScript, you might need to generate all possible combinations from multiple arrays. This is commonly known as the Cartesian product of arrays. Suppose we have two arrays of literals like these: const arr1 = ['a', 'b', 'c']; const arr2 = ['d', 'e', 'f']; console.log('Array 1:', arr1); console.log('Array 2:', arr2); Array 1: [ 'a', 'b', 'c' ] Array 2: [ 'd', 'e', 'f' ] We need a JavaScript function that takes multiple arrays and builds all possible combinations from them. For these two arrays, we want to get ...
Read MoreHow to merge two different array of objects using JavaScript?
Suppose, we have two different array of objects that contains information about the questions answered by some people: const arr1 = [ { PersonalID: '11', questionNumber: '1', value: 'Something' }, { PersonalID: '12', questionNumber: '2', value: 'whatever' }, { PersonalID: '13', questionNumber: '3', value: 'anything' }, { PersonalID: '14', questionNumber: '4', value: 'null' } ]; const arr2 = [ { questionNumber: '2', chID: '111', cValue: 'red' }, { questionNumber: '2', chID: '112', cValue: ...
Read MoreElements that appear twice in array in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appear exactly twice in the array and return a new array of those elements. Basic Approach Using Helper Function The first approach uses a helper function to count occurrences of each element: const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; ...
Read MoreJavaScript - Merge two arrays according to id property
When working with JavaScript arrays of objects, you often need to merge two arrays based on a common property like an ID. This is useful for combining related data from different sources, such as user profiles and addresses. Suppose we have two arrays of objects. The first contains user IDs and names, while the second contains user IDs and addresses: const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ...
Read MoreTrim and split string to form array in JavaScript
When working with comma-separated strings that contain unwanted whitespace, you need to trim spaces and split the string into an array. JavaScript provides multiple approaches to accomplish this task efficiently. Problem Statement Given a comma-separated string with irregular spacing: const str = "a, b, c, d , e"; console.log("Original string:", str); Original string: a, b, c, d , e We need to remove all whitespaces and split it into a clean array of elements. Method 1: Manual Space Removal This approach manually loops through the string to remove spaces ...
Read More