1

I want to total all same ID and assign a specific key

var ingredArray= [{"inventory_id":1,"pergram":222},{"inventory_id":1,"pergram":33},{"inventory_id":2,"pergram":424},{"inventory_id":2,"pergram":22},{"inventory_id":3,"pergram":400},{"inventory_id":5,"pergram":200}]
    
let deduction={};
ingredArray.forEach(function (item) {
     if (deduction.hasOwnProperty(item.inventory_id)) {
          deduction[item.inventory_id] = deduction[item.inventory_id] + parseFloat(item.pergram);
      } else {
          deduction[item.inventory_id] = parseFloat(item.pergram);
      }
});

console.log(deduction);

this is the result of my code

{1: 255, 2: 446, 3: 400, 5: 200}

I want to achieve

{"inventory_id":1,"pergram":255},{"inventory_id":2,"pergram":446},{"inventory_id":3,"pergram":400},{"inventory_id":5,"pergram":200}
2
  • I don't see a difference between the ingredArray and the final result that you want. As for your deduction vs the final result, that's not possible in a dictionary, it should have a proper key and a value. What you want as the final result is a sort of an array, and it looks exactly the same as ingredArray Commented Jul 30, 2021 at 2:18
  • There are two objects with the same key. The OP wants to integrate them. Commented Jul 30, 2021 at 2:20

3 Answers 3

2

Try this

var ingredArray = [{ "inventory_id": 1, "pergram": 222 }, { "inventory_id": 1, "pergram": 33 }, { "inventory_id": 2, "pergram": 424 }, { "inventory_id": 2, "pergram": 22 }, { "inventory_id": 3, "pergram": 400 }, { "inventory_id": 5, "pergram": 200 }]
           

var helper = {};
let deduction = ingredArray.reduce(function (r, o) {
var key = o.inventory_id;
    
if (!helper[key]) {
   helper[key] = Object.assign({}, o); // create a copy of o
   r.push(helper[key]);
} else {
   helper[key].pergram += o.pergram;
}
    
  return r;
}, []);
    
console.log(deduction);

Sign up to request clarification or add additional context in comments.

Comments

2

reduce over the array of objects building a new object of summed values based on key, and then grab the Object.values.

const data = [{"inventory_id":1,"pergram":222},{"inventory_id":1,"pergram":33},{"inventory_id":2,"pergram":424},{"inventory_id":2,"pergram":22},{"inventory_id":3,"pergram":400},{"inventory_id":5,"pergram":200}];

const out = data.reduce((acc, c) => {

  // Grab the id and pergram variables from the
  // new object in the iteration
  const { inventory_id: id, pergram } = c;

  // If the the accumulator object doesn't have a key that
  // matches the id create a new new object, and set the
  // pergram variable to zero
  acc[id] = acc[id] || { inventory_id: id, pergram: 0 };

  // And then add the pergram value to the
  // pergram object property
  acc[id].pergram += pergram;

  // Return the accumulator for the next iteration
  return acc;

}, {});

console.log(Object.values(out));

Comments

0

Use Map to store the object reference based on unique inventory_id and call Array.reduce() method over list of objects.

var ingredArray= [{"inventory_id":1,"pergram":222},{"inventory_id":1,"pergram":33},{"inventory_id":2,"pergram":424},{"inventory_id":2,"pergram":22},{"inventory_id":3,"pergram":400},{"inventory_id":5,"pergram":200}];

const processArray = (list) => {
  const map = new Map();
  return list.reduce((accumulator, obj) => {
    const inventory = map.get(obj.inventory_id);
    if (inventory) {
      inventory.pergram += obj.pergram;
    } else {
      accumulator.push(obj);
      map.set(obj.inventory_id, obj);
    }
    return accumulator;
  }, []);
}

console.log(processArray(ingredArray));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.