Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sum two objects javascript

var obj1 = {
  a: 12,
  b: 8,
  c: 17
};

var obj2 = {
  a: 12,
  b: 8,
  c: 17
};

var obj3 = {
  a: 12,
  b: 8,
  c: 17
};


function sumObjectsByKey(...objs) {
  return objs.reduce((a, b) => {
    for (let k in b) {
      if (b.hasOwnProperty(k))
        a[k] = (a[k] || 0) + b[k];
    }
    return a;
  }, {});
}

console.log(sumObjectsByKey(obj1, obj2, obj3));
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: invalid geojson object leaflet 
Javascript :: filter falsy values js 
Javascript :: longest word javascript 
Javascript :: array unique values javascript 
Javascript :: navigation reset 
Javascript :: pass data navigate react router dom 
Javascript :: package json proxy 
Javascript :: how to take a input video on browser using javascript and play it 
Javascript :: settimeout 
Javascript :: fetchData from json file 
Javascript :: how to add two elements in one path in react router v6 
Javascript :: between in mongodb 
Javascript :: angular convert response to json 
Javascript :: loop an array in javascript 
Javascript :: jQuery UI Sortable, then write order into a database 
Javascript :: js scrollintoview 
Javascript :: use local storage on server with node 
Javascript :: await inside map js 
Javascript :: iterate object keys javascript 
Javascript :: How to access the request body when POSTing using Node.js and Express 
Javascript :: javascript distance math 
Javascript :: react native open link in browser 
Javascript :: document.write multiple lines 
Javascript :: make keystore 
Javascript :: javascript backticks 
Javascript :: is react case sensitive 
Javascript :: angular maxlength directive input type number 
Javascript :: code for adding new elements in javascriipt js 
Javascript :: object element by index javascript 
Javascript :: javascript multiples of 3 and 5 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =