
- 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
Sum arrays repeated value - JavaScript
Suppose, we have an array of objects like this −
const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ];
We are required to add the values for all these objects together that have identical keys
Therefore, for this array, the output should be −
const output = [{'ID-01':4}, {'ID-02':8}];
We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the array.
Example
Following is the code −
const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ]; const indexOf = function(key){ return this.findIndex(el => typeof el[key] === 'number') }; Array.prototype.indexOf = indexOf; const groupArray = arr => { const res = []; for(let i = 0; i < arr.length; i++){ const key = Object.keys(arr[i])[0]; const ind = res.indexOf(key); if(ind !== -1){ res[ind][key] += arr[i][key]; }else{ res.push(arr[i]); }; }; return res; }; console.log(groupArray(arr));
Output
This will produce the following output in console −
[ { 'ID-01': 4 }, { 'ID-02': 8 } ]
- Related Articles
- Sum JavaScript arrays repeated value
- Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones
- Repeated sum of Number’s digits in JavaScript
- Reverse sum of two arrays in JavaScript
- Multiply and Sum Two Arrays in JavaScript
- Partial sum in array of arrays JavaScript
- Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?
- Column sum of elements of 2-D arrays in JavaScript
- Finding the sum of all common elements within arrays using JavaScript
- Sum all duplicate value in array - JavaScript
- MySQL query to sum rows having repeated corresponding Id
- Reverse index value sum of array in JavaScript
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- Javascript typed arrays
- JavaScript JSON Arrays

Advertisements