1

i have an array of objects that returns name: string and quantity: number, the thing is i have many objects into the array with the same name field value but different quantity values, so i want to show the total of all the quantities for each duplicated name. My code here:

const productsArray = [{ name: "lamp", quantity: 10 }, {name: "glass", quantity: 4}, {name:"lamp", quantity: 5}]

Lamp is repeated but it has different quantity values, so i want to merge those quantity values into one total quantity and show the name only once.

DESIRED RESULT

PRODUCT NAME: lamp PRODUCT QUANTITY: 15

PRODUCT NAME: glass PRODUCT QUANTITY: 4

Please help

4 Answers 4

1

Here is one option:-

const productsArray = [{ name: "lamp", quantity: 10 }, {name: "glass", quantity: 4}, {name:"lamp", quantity: 5}]


var output = [];

productsArray.forEach(function(item) {
  var existing = output.filter(function(v, i) {
    return v.name == item.name;
  });
  if (existing.length) {
    var existingIndex = output.indexOf(existing[0]);
    output[existingIndex].quantity += item.quantity;
  } else {
    output.push(item);
  }
});

console.dir(output);

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

Comments

0

I'd probably use a combination of reduce, Object.entries, and map to accomplish this.

const productsArray = [{ name: "lamp", quantity: 10 }, {name: "glass", quantity: 4}, {name:"lamp", quantity: 5}];

const combined = productsArray.reduce((acc, el) => {
  if (acc[el.name] === undefined) {
    acc[el.name] = 0;
  }
  acc[el.name] += el.quantity;
  return acc;
}, {});

const final = Object.entries(combined).map(([name, quantity]) => ({
  name,
  quantity
}));

console.log(final);

Comments

0

You mean something like this?

let productsObj = {};
for (let i = 0; i < productsArray.length; i++) {
  let item = productsArray[i];
  if (!!(productsObj[item.name])) {
    productsObj[item.name] += item.quantity;
  } else {
    productsObj[item.name] = item.quantity;
  }
}   

This way you can basically use an object as a key-value pair of items and access them using the name of the product (or its id, if you have it). To revert it back to a list you can do this:

const productsArray = [];
for (let name in productsObj) {
   productsArray.push({ name: name, quantity: productsObj[name] });
}

Comments

0

Alternative would be doing something like below: https://jsfiddle.net/yo2sad95/

const productsArray = [
  { name: 'lamp', quantity: 10 },
  { name: 'glass', quantity: 4 },
  { name: 'lamp', quantity: 5 },
]

const quantities = productsArray.reduce((accum, curr) => {
  const value = accum[curr.name] || 0
  return { ...accum, [curr.name]: value + curr.quantity }
}, {})

const result = Object.keys(quantities).map(product => ({ [product]: quantities[product] }))
console.warn(result)

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.