0

suppose we have an array of objects like this:

array = [{
  amount: 1000,
  cur: EUR
}, {
  amount: 1500,
  cur: GBD
}, {
  amount: 2000,
  cur: USD
}, {
  amount: 2500,
  cur: SSL
}]

Now I want to create a sum variable for this array which contains the sum of the amounts field whose cur isn't EUR and USD. To be more specific, I want a variable like sumOther which is equal to 1500 + 2500 = 4000. This variable contains the sum of the amounts whose cur is not EUR and USD.

4 Answers 4

2

You could just use a simple loop, testing the value of the cur on each iteration and adding to a total for the required values.

Or you could use the array .filter() and .reduce() methods:

var array = [
  {amount: 1000, cur: 'EUR'},
  {amount: 1500, cur: 'GBD'},
  {amount: 2000, cur: 'USD'},
  {amount: 2500, cur: 'SSL'}
]

var total = array.filter(function(v) { return !/EUR|USD/.test(v.cur) })
                 .reduce(function(acc, current) { return acc + current.amount }, 0)
                 
console.log(total)

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

Comments

2

Filter, reduce, and you're done.

var sumOther = array
                .filter(y => y.cur != EUR && y.cur != USD)
                .reduce((a, b) => a.amount + b.amount);



< ES6 version:

var sumOther = array
            .filter(function(y) { return y.cur != EUR && y.cur != USD })
            .reduce(function(a, b) { return a.amount + b.amount });

4 Comments

Note that this is ES6.
Sorry for the delay but this is not correct. If i have for example three different cur except from EUR and USD this implementation didnt work
It shows me NAN if i have three different currencies and null if i have one
Can you check this fiddle? jsfiddle.net/5e7qqzkk I replaced the GBD with 'GBD' so it can be tested. But in my original example I used GBD as a variable, and not as string, just like you did in your question, that might've been the problem.
1

You can use array map method

Hope this snippet will be useful

var sum = 0; // define a variable for adding amount
array.map(function(item){
    // check if currency is not EUR or GBD, then update the sum
    if(item.cur !=='EUR' && item.cur !=='GBD'){
    sum = sum+item.amount
    }

})
console.log(sum)

DEMO

2 Comments

If you're not using the return value from .map() you should be using .forEach(), because you're just iterating, not mapping.
@nnnnnn agree with you
0

Example where excluded strings are not hard coded:

const array = [{amount: 1000, cur: 'EUR'}, 
{amount: 1500, cur: 'GBD'}, 
{amount: 2000, cur: 'USD'}, 
{amount: 2500, cur: 'SSL'}];

const exclude = ['EUR', 'USD'];

var sum = array.map(x => (!exclude.includes(x.cur)) ? x.amount : false)
               .reduce((a, b) => a + b);

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.