2

I wanna sum the elements of an array, but not all. Let's say that my array is:

var example = [
                   {"id": 1, "value": 50, "active": true}, 
                   {"id": 2, "value": 70, "active": false}, 
                   {"id": 3, "value": 45, "active": true}, 
                   {"id": 4, "value": 50, "active": false}
               ];

What I need to do is sum just the values of the elements with "active":true.

There's a way to do that? Maybe is too simple, but right now, my brain is dead.

I'm using Javascript and AngularJs.

Thanx in advance.

4 Answers 4

4

You can use filter and reduce like so:

var a = [{"id": 1, "value": 50, "active": true}, {"id": 2, "value": 70, "active": false}, {"id": 3, "value": 45, "active": true}, {"id": 4, "value": 50, "active": false}]

var result = a.filter(o => o.active).reduce((acc, cur) => cur.value + acc, 0)

console.log(result)

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

1 Comment

@ChuckVillavicencio glad to help!
2

You can prefer a simple syntax...

var example = [{"id": 1, "value": 50, "active": true}, {"id": 2, "value": 70, "active": false}, {"id": 3, "value": 45, "active": true}, {"id": 4, "value": 50, "active": false}];
var sum = 0

example.forEach(function(item, index){
  if (item["active"] == true) sum += item["value"];
});

console.log(sum);

1 Comment

Good! Neve thought to use forEach
1

You can just use reduce, using the fact that in a numeric context true = 1 and false = 0:

var a = [{"id": 1, "value": 50, "active": true}, {"id": 2, "value": 70, "active": false}, {"id": 3, "value": 45, "active": true}, {"id": 4, "value": 50, "active": false}]

var result = a.reduce((a, c) => a + c.value * c.active, 0)

console.log(result)

Or if you prefer, using the ternary operator:

var a = [{"id": 1, "value": 50, "active": true}, {"id": 2, "value": 70, "active": false}, {"id": 3, "value": 45, "active": true}, {"id": 4, "value": 50, "active": false}]

var result = a.reduce((a, c) => a + (c.active ? c.value : 0), 0)

console.log(result)

1 Comment

Cool the fact of numeric value... didn't think about it
0

I suggest you to use lodash If you need operation on array or collection

import lodash in file where you want to use it

import _ from 'lodash';
// or
const _ = require('lodash');

Apply filter and sumBy

var example = [
 {"id": 1, "value": 50, "active": true}, 
 {"id": 2, "value": 70, "active": false},
 {"id": 3, "value": 45, "active": true}, 
 {"id": 4, "value": 50, "active": false}
];
//Filter all active value
const filterData = _.filter(example, "active");
// const filterData = [
//  {"id": 1, "value": 50, "active": true}, 
//  {"id": 3, "value": 45, "active": true}, 
// ];

//Sum all value filter data
const sum = _.sumBy(filterData, "value");

console.log(sum)

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.