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 133 of 534
Highest occurrence in an array or first selected in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences. Problem Statement Given an array of values, find the element that appears most frequently. If multiple elements have the same highest frequency, return the one that appears first in the array. const arr1 = ['25', '50', 'a', 'a', 'b', 'c']; // 'a' appears 2 times (most frequent), so return 'a' ...
Read MoreFind all substrings combinations within arrays in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements. For example − If the array is − const arr = ["abc", "abcd", "abcde", "xyz"]; Then the output should be − const output = ["abc", "abcd", "abcde"]; because the first two are the substring of the last one. How It Works The algorithm compares each string with every other string in the ...
Read MoreReturn a sorted array in lexicographical order in JavaScript
We are required to write a JavaScript function that takes two arrays, say arr1 and arr2. Our function should return a sorted array in lexicographical order of the strings of arr1 which are substrings of strings of arr2. How It Works The function iterates through arr1 and checks if each string is a substring of any string in arr2. If found, it adds the string to the result array and continues to the next string using a labeled continue statement. Example The code for this will be − const lexicographicalSort = (arr1 = [], ...
Read MoreTest for existence of nested JavaScript object key in JavaScript
Testing for the existence of nested JavaScript object keys is a common requirement when working with complex data structures. This prevents errors when accessing deeply nested properties that may not exist. The Problem Accessing nested properties directly can throw errors if intermediate keys don't exist: let test = {}; // This would throw an error: // console.log(test.level1.level2.level3); // TypeError: Cannot read property 'level2' of undefined Using a Custom Function We can create a function that safely checks each level of nesting: const checkNested = function(obj = {}){ ...
Read MoreCounting the occurrences of JavaScript array elements and put in a new 2d array
We are required to write a JavaScript function that takes in an array of literal values. The function should then count the frequency of each element of the input array and prepare a new array on that basis. For example − If the input array is − const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; Then the output should be − const output = [ [5, 3], [2, 5], [9, 1], [4, 1] ]; ...
Read MoreTransform data from a nested array to an object in JavaScript
Suppose, we have the following array of arrays: const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ]; We are required to write a JavaScript function that takes in one such nested array. The function should then prepare an object based on the array. The object for the above array should look like: const output = [ {dog: 'Harry', ...
Read MoreBuild tree array from flat array in JavaScript
Converting a flat array into a hierarchical tree structure is a common task when working with JSON data. This process transforms an array where each item has an id and parentId into a nested structure with parent-child relationships. Every entry in the JSON array has: id — a unique identifier parentId — the id of the parent node (which is "0" for root nodes) level — the depth level in the tree hierarchy The JSON data is already ordered, meaning each entry will ...
Read MoreConvert JSON array into normal json in JavaScript
Sometimes you receive data in a complex JSON array format with key-value pairs, but need to convert it to a simple flat object structure for easier access and manipulation. Problem Structure Consider this complex JSON array with nested key-value objects: const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": ...
Read MoreGrouping an Array and Counting items creating new array based on Groups in JavaScript
When working with arrays of objects in JavaScript, you often need to group data by specific properties and count unique items. This article demonstrates how to group an array by region and count unique users per region. Problem Statement Suppose we have an array of objects representing user data across different regions: const arr = [ { region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, ...
Read MoreBuild tree array from JSON in JavaScript
Building a tree structure from a flat array is a common task in JavaScript. When you have hierarchical data represented as a flat array with codes indicating parent-child relationships, you can transform it into a nested tree structure. The Problem Suppose we have the following flat array where the code property indicates hierarchy through dot notation: const arr = [{ "code": "2", "name": "PENDING" }, { "code": "2.2", "name": "PENDING CHILDREN" }, { "code": "2.2.01.01", ...
Read More