JSON Articles

Page 3 of 16

Compare keys & values in a JSON object when one object has extra keys in JavaScript

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

When comparing JSON objects in JavaScript, you often need to check if the common keys have matching values, even when one object has extra properties. This is useful for validating partial object matches or checking if one object is a subset of another. Problem Statement Consider these two objects: const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"}; We need a function that returns true because all common keys (a, b, c) have matching values, ignoring the extra keys (e, d) ...

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

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

Calculate average from JSON data based on multiple filters JavaScript

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

When working with JSON data, you often need to filter, group, and calculate averages based on multiple criteria. This article demonstrates how to group objects by multiple fields and compute averages while handling edge cases like undefined values. Problem Statement Given an array of supplier objects, we need to: Group objects with the same "SupplierName" and "Category" Sum their points together (ignoring undefined values) Calculate the average points for each group Return the grouped results with totals and averages ...

Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 536 Views

Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...

Read More

Convert JSON array into normal json in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 819 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

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

Flattening a JSON object in JavaScript

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

Flattening a JSON object means converting a nested object structure into a single-level object where nested keys are represented using dot notation. This technique is useful for data processing, form handling, and API transformations. Suppose we have the following JSON object that may contain nesting up to any level: const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 ...

Read More

How to add an element to a JSON object using JavaScript?

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

In this article, you will understand how to add an element to a JSON object using JavaScript. JSON objects are key-value pairs separated by colons and surrounded by curly braces {}. JavaScript provides two main ways to add new properties: bracket notation and dot notation. Using Bracket Notation Bracket notation is useful when property names contain special characters or are stored in variables. var jsonObject = { members: { host: "hostName", viewers: ...

Read More

Adding a unique id for each entry in JSON object in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 5K+ Views

In this problem statement, our task is to add a unique id for every object entry in a JSON object with the help of JavaScript. We will use a loop and a variable to store the id for each object in the JSON object. Understanding the Problem Statement The problem statement is to write a function in JavaScript that adds a unique id to every item in the given JSON object. For example, if we have: let obj = { "entry1": {empName: "A", age: 25}, "entry2": {empName: "B", age: 30} ...

Read More
Showing 21–30 of 152 articles
« Prev 1 2 3 4 5 16 Next »
Advertisements