1

I have a application where I need count my doors. For example:

  • door1 has 60 people
  • door2 has 70 people

But how can I get the sum of each door because. I want push the sum of each door to my db, this data I get from my client every minute then I push the results to my db, and I clear the array for a next set of objects.

var a = [
    {door: 1 , people: 20},
    {door: 2 , people: 20},
    {door: 1 , people: 10},
    {door: 1 , people: 20},
    {door: 2 , people: 50},
]

6 Answers 6

2

This a great example of when you can use map() and reduce() together:

Array.prototype.map() will run a function on each item in an array and transform each item into something else (returning a new array of the same length).

Array.prototype.reduce() will cumulatively calculate a single value based on each value in an array (returning just a single value).

var total = a.map(function(e) {
    return e.people
})
.reduce(function(a, b) {
  return {a + b};
})

In the example above we first use map() to transform each object in the array into just its 'people' value. So after this step we have an array that looks like:

[20, 20, 10, 20, 50]

Then we call reduce() on that array, which cumulatively adds the numbers together.

In ES6, this can be written even more succintly as:

let total = a.map(o => o.people).reduce((a,b) => a + b);
Sign up to request clarification or add additional context in comments.

Comments

1

var a = [
    {door: 1 , people: 20},
    {door: 2 , people: 20},
    {door: 1 , people: 10},
    {door: 1 , people: 20},
    {door: 2 , people: 50},
];
var sum = {};
for(var i=0;i<a.length;i++){
       sum[a[i].door] = sum[a[i].door] || 0;
       sum[a[i].door] += a[i].people;
}
console.log(sum);

Comments

0
var result = {}    
a.map(i => result[i.door] = (result[i.door] || 0) + i.people)

and now just console.log(result)

Or you can even enhance the code using some syntactic sugar like that:

var result = {}    
    a.map({door, people} => result[i.door] = (result[door] || 0) + people)

Comments

0
values=[1,2].map(door=>a.filter(d=>d.door==door).reduce((val,d)=>val+d.people,0));

values will be [60,70], For every door (1 and 2), get all elems of a where the elems door is door, then join these elems people value and map it back to the array. So every door is replaced with its referring value.

If you dont know the doors, you could create an associative object:

 values=a.reduce((all,elem)=>{ return all[elem[door]]=all[elem.door]||0,all[elem.door]+=elem.people;},{});

this will result in:

{
1:60,
2:70
}

Comments

0
var array = [
{door: 1 , people: 20},
{door: 2 , people: 20},
{door: 1 , people: 10},
{door: 1 , people: 20},
{door: 2 , people: 50},
]

var res = [];

array.forEach(function(element) {
var e = res.find(function(data) {
    return data.door == element.door;
});

if(e) {
  element.people  =  e.people + element.people;
} else {
 res.push({
   door: element.door,
   people: element.people
 });
}   
});

Comments

0

I don't know your desired result format, but if an object is good and taking into account you don't know about your doors beforehand, using just reduce() could be enough.

var a = [
    {door: 1 , people: 20},
    {door: 2 , people: 20},
    {door: 1 , people: 10},
    {door: 1 , people: 20},
    {door: 2 , people: 50},
]

var result = a.reduce((accumulator, currentValue) => {
  if (!accumulator[currentValue.door]) {
    // set property the first time you find a door
    accumulator[currentValue.door] = 0;
  }
  // sum the number of people each door in each iteration
  accumulator[currentValue.door] += currentValue.people;
  return accumulator;
}, {});

console.log(result)

The code above is easier to understand, but is a bit verbose. The callback of reduce() can be abbreviated like this:

a.reduce((accumulator, currentValue) => {
  accumulator[currentValue.door] = accumulator[currentValue.door] || 0;
  accumulator[currentValue.door] += currentValue.people;
  return accumulator;
}, {});

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.