Object Oriented Programming Articles

Page 52 of 589

Test for existence of nested JavaScript object key in JavaScript

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

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 More

Counting the occurrences of JavaScript array elements and put in a new 2d array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

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 More

Transform data from a nested array to an object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 838 Views

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 More

Build tree array from flat array in JavaScript

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

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 More

Convert JSON array into normal json in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 820 Views

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 More

Grouping an Array and Counting items creating new array based on Groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

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 More

Build tree array from JSON in JavaScript

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

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

Nested collection filter with JavaScript

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

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 More

Create an object based on 2 others in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

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 More

Data manipulation with JavaScript

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

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
Showing 511–520 of 5,881 articles
« Prev 1 50 51 52 53 54 589 Next »
Advertisements