1

I have been stuck on this for 8 days now.

Let's say here is my response object array:

var items = [
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'hp-44',
    price: 100,
    id: 10,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
]

So far i have managed to make a function using reduce to accumulate the price. Here is the code that does this:

var obj = items.reduce( function (allItems, currentItem) {
var item_name = currentItem.name;
var item_price = currentItem.price;


var total_prices = allItems[item_price] = (allItems[item_price] || 0) +   
item_price;


var result = {};

result[item_name] = {
    item_price:  total_prices,
}


return Object.assign(allItems, result);
}, {} )

Although this works, it does not return the results i want, instead it returns this:

{
'100': 100,
'200': 600,
'250': 500,
'dell-66': { item_price: 600 },
'hp-44': { item_price: 100 },
'acer-33': { item_price: 500 } 
 }

Here is what i intended and need it to be (without the extra key/values at the top):

{
'dell-66': { item_price: 600 },
'hp-44': { item_price: 100 },
'acer-33': { item_price: 500 } 
 }

How Can i achieve this please?

0

3 Answers 3

7

You could do it like this.

const items = [{
    name: 'dell-66',
    price: 200,
    id: 12,
  }, {
    name: 'hp-44',
    price: 100,
    id: 10,
  }, {
    name: 'acer-33',
    price: 250,
    id: 33,
  }, {
    name: 'dell-66',
    price: 200,
    id: 12,
  }, {
    name: 'acer-33',
    price: 250,
    id: 33,
  }, {
    name: 'dell-66',
    price: 200,
    id: 12,
  },
].reduce((res, obj) => {
  res[obj.name] = { 
    item_price: (obj.name in res ? res[obj.name].item_price : 0) + obj.price 
  }
  return res;
}, {})

console.log(items);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Holy Moly! This Works! Thank you! But could you maybe explain what had been the problem with my code?
@ViveeOmomi Well, to start off with, allItems[item_price] is looking for a key of item_price in allItems. Aka: { '100': value} not (As i think you were going for) a key where its value is item_price, Aka: { key: 100 }
3

You can use following code:

var items = [
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'hp-44',
    price: 100,
    id: 10,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
]

let result = items.reduce((result, current) => {
	if(!result[current.name]){
		result[current.name] = { item_price: 0 };
	}
	result[current.name].item_price += current.price;
	return result;
}, {})

console.log(result);

Comments

3

You can use the map function for this. Please try following code.

var items = [
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'hp-44',
    price: 100,
    id: 10,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
{
    name: 'acer-33',
    price: 250,
    id: 33,
},
{
    name: 'dell-66',
    price: 200,
    id: 12,
},
]

var rlt = {};

items.map(item => {
  let price = item.price
  let name = item.name
  if (rlt[name]) {
    rlt[name] = rlt[name] + price
  } else {
    rlt[name] = price;
  }
});
console.log(rlt);

6 Comments

Why not use Array#reduce?
we can use reduce too for this. but I was going to show several solution.
I'm aware :) I'm just asking why you opted not to. I'm curious.
Thank You, I am So glad you answered, i was wondering if Array#map could ever do this, now i know better, glad to have diverse solutions.
@ViveeOmomi I'm glad this could help you.
|

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.