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
Object Oriented Programming Articles
Page 52 of 589
Test 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 MoreNested collection filter with JavaScript
Filtering nested collections in JavaScript requires searching through objects that contain arrays of other objects. Let's explore how to filter an array based on properties within nested objects. The Problem Suppose we have an array of nested objects where each object contains a legs array with carrier information: const arr = [{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 2, legs:[{ carrierName: 'SunExpress' }, ...
Read MoreCreate an object based on 2 others in JavaScript
In JavaScript, you often need to combine properties from multiple objects into a new one. There are several modern approaches to achieve this without modifying the original objects. Problem Statement Given two objects with properties and methods, we want to create a third object that contains all properties from both: const a = { a: 1, af: function() { console.log(this.a) }, }; const b = { b: 2, bf: function() { console.log(this.b) }, }; // Goal: Create object ...
Read MoreData manipulation with JavaScript
When working with data arrays, you often need to combine different datasets. This tutorial shows how to merge a months array with a cashflows array to create a complete dataset with all months represented. Problem Statement Suppose we have two arrays describing cashflow data: const months = ["jan", "feb", "mar", "apr"]; const cashflows = [ {'month':'jan', 'value':10}, {'month':'mar', 'value':20} ]; console.log("Months:", months); console.log("Cashflows:", cashflows); Months: [ 'jan', 'feb', 'mar', 'apr' ] Cashflows: [ { month: 'jan', value: 10 }, { month: 'mar', value: 20 ...
Read More