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 124 of 534
Get the total number of same objects in JavaScript
When working with arrays of objects in JavaScript, you often need to count how many objects share the same property values. This is useful for analyzing data like flight routes, user preferences, or categorizing items. Suppose we have an array of objects describing flight routes: const routes = [ { flyFrom: "CDG", flyTo: "DUB", return: 0, }, { ...
Read MoreSorting parts of array separately in JavaScript
We have an array that contains many objects. We are required to write a function to sort the first half of the array in ascending order and the second half of the array in ascending order as well, but without inter-mixing the entries of halves into one another. Consider this sample array: const arr = [ {id:1, x: 33}, {id:2, x: 22}, {id:3, x: 11}, {id:4, x: 3}, {id:5, x: 2}, {id:6, x: ...
Read MoreJavaScript - Find keys for the matched values as like query in SQL
In JavaScript, you can filter object properties based on value matching, similar to SQL's WHERE clause with LIKE operator. This is useful for searching through data structures and finding keys that match specific criteria. Problem Statement Given an object with key-value pairs, we need to find all keys whose values contain a specific search term (case-insensitive). const obj = { "100": "Jaipur", "101": "Delhi", "102": "Raipur", "104": "Goa" }; We want to create a function that returns ...
Read MoreList all duplicate values in a nested JavaScript object
When working with nested JavaScript objects, you may need to find duplicate values that appear at different levels of nesting. This tutorial shows how to recursively search through a nested object and identify all duplicate values. Problem Statement Consider a nested object containing pet information: const pets = { owner1: 'Frank', owner2: 'Curly', owner3: 'Maurice', dogs: { terriers: { name1: ...
Read MoreSearch by id and remove object from JSON array in JavaScript
Suppose, we have an array of objects that contains data about some movies like this − const arr = [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "action"}, {id: "4", name: "Ordinary People", type: "drama"}, {id: "5", name: "Billy Elliot", type: "drama"}, {id: "6", name: "Toy Story", type: "children"} ]; We are required to write ...
Read MoreJavaScript: Sort Object of Objects
Suppose we have an Object of Objects like this: const obj = { "CAB": { name: 'CBSSP', position: 2 }, "NSG": { name: 'NNSSP', position: 3 }, "EQU": { name: 'SSP', position: 1 } }; We need to write a JavaScript function that sorts the sub-objects based on the 'position' property in ascending order. Method 1: Using Object.keys() and ...
Read MoreGroup Similar Items in JSON in JavaScript
Suppose, we have a JSON Array that contains data about some tickets like this: const arr = [ { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": ...
Read MoreHow to find all partitions of a multiset, where each part has distinct elements in JavaScript
Finding all partitions of a multiset where each part contains distinct elements is a complex combinatorial problem. We need to create an algorithm that generates all possible ways to divide elements into groups without repetition within each group. Let's say we have an array with repeated elements: const arr = [A, A, B, B, C, C, D, E]; We need to find all combinations that use the entire array, where no elements are repeated within each partition. Example Partitions [A, B, C, D, E] [A, B, C] [A, B, C, D] [A, ...
Read MoreFind all occurrences of a word in array in JavaScript
When working with arrays in JavaScript, you might need to find how many elements contain a specific word or substring. This article demonstrates multiple approaches to count occurrences of a word within array elements. Problem Statement We need to write a JavaScript function that takes an array of strings as the first argument and a search word as the second argument. The function should return the count of array elements that contain the specified word. Method 1: Using filter() and indexOf() This approach uses filter() to find matching elements and returns the length of the filtered ...
Read MoreSorting array of strings having year and month in JavaScript
Suppose, we have an array of strings that contains month-year combined strings like this: const arr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"]; We need to write a JavaScript function that takes such an array and sorts these dates from oldest to latest order. Approach The solution involves creating a custom sorting function that: Splits each date string to separate year and month Converts month names to numeric values ...
Read More