Javascript Articles

Page 124 of 534

Get the total number of same objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

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 More

Sorting parts of array separately in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

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 More

JavaScript - Find keys for the matched values as like query in SQL

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

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 More

List all duplicate values in a nested JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Search by id and remove object from JSON array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

JavaScript: Sort Object of Objects

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Group Similar Items in JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

How to find all partitions of a multiset, where each part has distinct elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

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 More

Find all occurrences of a word in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Sorting array of strings having year and month in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

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
Showing 1231–1240 of 5,340 articles
« Prev 1 122 123 124 125 126 534 Next »
Advertisements