
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Merge JavaScript objects with the same key value and count them
Suppose, we have an array of objects like this −
const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }];
We are required to write a JavaScript function that takes in one such array.
The function should then merge all those objects together that have the common value for "id" property.
Therefore, for the above array, the output should look like −
const output = [{ "value": 10, "id": "111", "name": "BlackCat", "count": 2, }, { "value": 15, "id": "777", "name": "WhiteCat", "count": 1, }]
Example
const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; const combinedItems = (arr = []) => { const res = arr.reduce((acc, obj) => { let found = false; for (let i = 0; i < acc.length; i++) { if (acc[i].id === obj.id) { found = true; acc[i].count++; }; } if (!found) { obj.count = 1; acc.push(obj); } return acc; }, []); return res; } console.log(combinedItems(arr));
Output
And the output in the console will be −
[ { value: 10, id: '111', name: 'BlackCat', count: 2 }, { value: 15, id: '777', name: 'WhiteCat', count: 1 } ]
- Related Articles
- Merge objects in array with similar key JavaScript
- Check how many objects are in the array with the same key in JavaScript
- Group all the objects together having the same value for the '_id' key in JavaScript
- Accessing nested JavaScript objects with string key
- JavaScript program to merge two objects into a single object and adds the values for same keys
- Find specific key value in array of objects using JavaScript
- How to merge two JavaScript objects?
- How to Sort object of objects by its key value JavaScript
- How to merge objects into a single object array with JavaScript?
- How to merge two arrays with objects in one in JavaScript?
- JavaScript Sum of two objects with same properties
- How to create an array with multiple objects having multiple nested key-value pairs in JavaScript?
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- Merge two objects in JavaScript ignoring undefined values
- How to add values in columns having same name and merge them in R?

Advertisements