Web Development Articles

Page 223 of 801

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 283 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 287 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

Compute cartesian product of elements in an array in JavaScript

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

The Cartesian product of two sets (arrays) A and B, denoted A × B, is the set (array) of all ordered pairs (a, b) where a is in A and b is in B. In simpler terms, a cartesian product of two arrays is a permutation of all possible arrays of two elements whose first element belongs to the first array and the second element belongs to the second array. Example of Cartesian Product If the two arrays are: const arr1 = [1, 2, 3]; const arr2 = [4, 5]; Then their cartesian ...

Read More

Group by JavaScript Array Object

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

Suppose we have an array of arrays that contains the marks of some students in some subjects like this − const arr = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]; We are required to write a JavaScript function that takes in one such array and returns an object of objects. The return object should contain an object for each unique subject, and that object should contain information like the number of appearances ...

Read More

How to count the occurrence of a specific string in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

In JavaScript, counting occurrences of a substring within a string is a common task. There are several approaches to accomplish this, from simple loops to built-in string methods. For example, counting how many times "is" appears in "this is a string" should return 2. count('this is a string', 'is') should return 2; Method 1: Using indexOf() with Loop The most reliable approach uses indexOf() to find each occurrence and increment a counter: const str1 = 'this is a string'; const str2 = 'is'; function countOccurrences(mainStr, subStr) { ...

Read More

Iterating through an array, adding occurrences of a true in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

Suppose we have an array of true/false values represented by 't'/'f' which we retrieved from some database like this − const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; console.log(arr); [ 'f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't' ] We need to count consecutive occurrences of 't' that are sandwiched between two 'f's and return an array of those counts. Array: ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', ...

Read More

Find specific key value in array of objects using JavaScript

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

When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data. Example Data Structure Consider this product catalog object where each category contains an array of products: const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" ...

Read More
Showing 2221–2230 of 8,010 articles
« Prev 1 221 222 223 224 225 801 Next »
Advertisements