1

I have something like this:

tires: [{
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct2",
  quantity: 1
}];

What I'm trying to accomplish is

tires: [{
  name: "fancyProduct1",
  quantity: 3
}, {
  name: "fancyProduct2",
  quantity: 1
}]

Any ideas on best way to approach this?

6
  • The main idea is to group products by name and sum their quantities. "have a quantity of 1" thanks that was a typo Commented Jun 22, 2018 at 7:51
  • 1
    Please help me understand your question. Are you trying to aggregate data? If it is for reporting purposes, there are several reporting tool which can help you. Commented Jun 22, 2018 at 7:52
  • I'm trying to restructure existing array of objects into the new ones Commented Jun 22, 2018 at 7:53
  • 1
    @BobanStanojevic Have you checked my soluton below? Commented Jun 22, 2018 at 10:06
  • 1
    Yes, @chŝdk Thank you! Commented Jun 22, 2018 at 10:11

4 Answers 4

4

You can use reduce to group the array into one object. Use Object.values to convert the object into an array.

let tires = [{"name":"fancyProduct1","quantity":1},{"name":"fancyProduct1","quantity":1},{"name":"fancyProduct1","quantity":1},{"name":"fancyProduct2","quantity":1}];

let result = Object.values(tires.reduce((c, {name,quantity}) => {
  c[name] = c[name] || {name,quantity: 0}
  c[name].quantity += quantity;
  return c;
}, {}));

console.log(result);

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

Comments

1

Using Reduce will accomplish this:

var products = { tires: [ {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct2", quantity: 1}] };

var result = products.tires.reduce((acc,current) => {

    if (!acc[current.name]) {
       acc[current.name] = { name: current.name, quantity: 0};
    }

    acc[current.name].quantity++;

    return acc;
}, {});

var resultArray = Object.values(result);
console.log(resultArray);

Comments

1

Well you can use a simple Array.forEach() call to loop over the array items, along with Array.find() to check for the existence of your iterated item in the result array and do your logic accordingly.

This is how should be your code:

var result = [];

tires.forEach(function(el) {
  let found = result.find(o => o.name === el.name);
  if (found) {
    found["quantity"] += el["quantity"];
  } else {
    result.push(el);
  }
});

Demo:

var tires = [{
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct1",
  quantity: 1
}, {
  name: "fancyProduct2",
  quantity: 1
}];

var result = [];

tires.forEach(function(el) {
  let found = result.find(o => o.name === el.name);
  if (found) {
    found["quantity"] += el["quantity"];
  } else {
    result.push(el);
  }
});

console.log(result);

Comments

-1

You can do something like this...

var newTries = tires.map(n => (
 // logic for new array where you can get attributes of item in tires to create a new array. 
console.log(n);  // this can show what properties are available in the current item write it to the console. 
)};

Hope this helps.

1 Comment

Please add the logic for new array where you can get attributes of item in tires to create a new array. to actually answer this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.