Get the total value of the matching array elements

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












This is my current array



0:modelNumber: "123456789", balance: amount:1000, currency:"EUR" 
1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
7:modelNumber: "854300564", balance: amount:3500, currency:"USD"


I'm trying to return a new array, with each currecny and the total value for each currecny.



Like below return the total amount for each currency in the array above



0:currency: "EUR", totalAmount: 3500
1:currency: "GBP", totalAmount: 5000
2:currency: "USD", totalAmount: 4500
3:currency: "INR", totalAmount: 6000


My approach initially



//the current array
let theInitialArray = state.account;

const results = theInitialArray.reduce((accumalator, current) =>
const currency = current.balance;
if (accumalator[currency])
accumalator[currency].push(current);
return accumalator;

accumalator[currency] = [current];
return accumalator;

, );

let frank = Object.keys(results)
let jim = ;
let expectedOutput = theInitialArray.filter((x) =>

for (let i=0; i < frank.length; i++)
if (x.balance.currency === frank[i])
jim.push('currency': frank[i], 'amount': x.balance.amount);


);
console.log('expectedOutput', expectedOutput)
return expectedOutput









share|improve this question



























    up vote
    6
    down vote

    favorite












    This is my current array



    0:modelNumber: "123456789", balance: amount:1000, currency:"EUR" 
    1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
    2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
    3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
    4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
    5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
    6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
    7:modelNumber: "854300564", balance: amount:3500, currency:"USD"


    I'm trying to return a new array, with each currecny and the total value for each currecny.



    Like below return the total amount for each currency in the array above



    0:currency: "EUR", totalAmount: 3500
    1:currency: "GBP", totalAmount: 5000
    2:currency: "USD", totalAmount: 4500
    3:currency: "INR", totalAmount: 6000


    My approach initially



    //the current array
    let theInitialArray = state.account;

    const results = theInitialArray.reduce((accumalator, current) =>
    const currency = current.balance;
    if (accumalator[currency])
    accumalator[currency].push(current);
    return accumalator;

    accumalator[currency] = [current];
    return accumalator;

    , );

    let frank = Object.keys(results)
    let jim = ;
    let expectedOutput = theInitialArray.filter((x) =>

    for (let i=0; i < frank.length; i++)
    if (x.balance.currency === frank[i])
    jim.push('currency': frank[i], 'amount': x.balance.amount);


    );
    console.log('expectedOutput', expectedOutput)
    return expectedOutput









    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      This is my current array



      0:modelNumber: "123456789", balance: amount:1000, currency:"EUR" 
      1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
      2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
      3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
      4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
      5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
      6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
      7:modelNumber: "854300564", balance: amount:3500, currency:"USD"


      I'm trying to return a new array, with each currecny and the total value for each currecny.



      Like below return the total amount for each currency in the array above



      0:currency: "EUR", totalAmount: 3500
      1:currency: "GBP", totalAmount: 5000
      2:currency: "USD", totalAmount: 4500
      3:currency: "INR", totalAmount: 6000


      My approach initially



      //the current array
      let theInitialArray = state.account;

      const results = theInitialArray.reduce((accumalator, current) =>
      const currency = current.balance;
      if (accumalator[currency])
      accumalator[currency].push(current);
      return accumalator;

      accumalator[currency] = [current];
      return accumalator;

      , );

      let frank = Object.keys(results)
      let jim = ;
      let expectedOutput = theInitialArray.filter((x) =>

      for (let i=0; i < frank.length; i++)
      if (x.balance.currency === frank[i])
      jim.push('currency': frank[i], 'amount': x.balance.amount);


      );
      console.log('expectedOutput', expectedOutput)
      return expectedOutput









      share|improve this question















      This is my current array



      0:modelNumber: "123456789", balance: amount:1000, currency:"EUR" 
      1:modelNumber: "987654321", balance: amount:2000, currency:"EUR"
      2:modelNumber: "322353466", balance: amount:1500, currency:"GBP"
      3:modelNumber: "892347522", balance: amount:1000, currency:"USD"
      4:modelNumber: "931883113", balance: amount:3000, currency:"INR"
      5:modelNumber: "854300564", balance: amount:2500, currency:"GBP"
      6:modelNumber: "931883113", balance: amount:3000, currency:"INR"
      7:modelNumber: "854300564", balance: amount:3500, currency:"USD"


      I'm trying to return a new array, with each currecny and the total value for each currecny.



      Like below return the total amount for each currency in the array above



      0:currency: "EUR", totalAmount: 3500
      1:currency: "GBP", totalAmount: 5000
      2:currency: "USD", totalAmount: 4500
      3:currency: "INR", totalAmount: 6000


      My approach initially



      //the current array
      let theInitialArray = state.account;

      const results = theInitialArray.reduce((accumalator, current) =>
      const currency = current.balance;
      if (accumalator[currency])
      accumalator[currency].push(current);
      return accumalator;

      accumalator[currency] = [current];
      return accumalator;

      , );

      let frank = Object.keys(results)
      let jim = ;
      let expectedOutput = theInitialArray.filter((x) =>

      for (let i=0; i < frank.length; i++)
      if (x.balance.currency === frank[i])
      jim.push('currency': frank[i], 'amount': x.balance.amount);


      );
      console.log('expectedOutput', expectedOutput)
      return expectedOutput






      javascript arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Alex Peters

      567414




      567414










      asked 2 hours ago









      themanwiththemasterplan

      715




      715






















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          3
          down vote













          Here is a O(n) approach of getting that output:



          1. You first define a empty object tempObj which will be used to store the currency and totalAmount value as a object based on the currency key

          2. Then, if this currency key is defined in the tempObj you will simply add the amount with the totalAmount for an existing object.

          3. Else you will create a object with the amount as totalAmount, currency as key of tempObj and currency as currency of the item in forEach loop

          4. Finally you will need to do Object.values(tempObj) so that we get the object values and ignore the keys of tempObj to get the desired result.




          var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
          modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
          modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
          modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
          modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
          modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
          modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
          modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

          var tempObj = ;
          arr.forEach((obj)=>
          if(tempObj[obj.balance.currency])
          tempObj[obj.balance.currency].totalAmount += obj.balance.amount
          else
          tempObj[obj.balance.currency] =
          currency: obj.balance.currency,
          totalAmount : obj.balance.amount


          );
          var resArray = Object.values(tempObj);
          console.log(resArray);








          share|improve this answer
















          • 2




            This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
            – malbarmawi
            2 hours ago

















          up vote
          2
          down vote













          You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:






          const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

          const result = Object.values(data.reduce((r, balance ) =>
          const amount, currency = balance;
          if(!r[currency]) r[currency] = currency, amount: 0 ;

          r[currency].amount += amount;

          return r;
          , ));

          console.log(result);








          share|improve this answer



























            up vote
            0
            down vote













            It's possible to reduce the values you have and them map them into the format you require:






            const values = [
            modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
            modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
            modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
            modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
            modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
            modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
            modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
            modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
            ];

            const results = values.reduce((prev, curr) => (
            ...prev,
            [curr.balance.currency]: (prev[curr.balance.currency] ), )

            const inCorrectFormat = Object.keys(results).map(key => (
            currency: key,
            totalAmount: results[key]
            ))

            console.dir(inCorrectFormat)








            share|improve this answer



























              up vote
              0
              down vote













              I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:






              let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

              let balanceArray = initialArray.map(el => el.balance);

              let finalObject = balanceArray.reduce((acc, el) =>
              if(acc[el.currency]) acc[el.currency] += el.amount
              else acc[el.currency] = el.amount
              return acc;
              ,);

              let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


              console.log(finalArray)








              share|improve this answer



























                up vote
                0
                down vote













                You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.






                var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                result = Array.from(
                data.reduce((m, balance: amount, currency ) =>
                m.set(currency, (m.get(currency) || 0) + amount), new Map),
                ([currency, totalAmount]) => ( currency, totalAmount )
                );

                console.log(result);

                .as-console-wrapper max-height: 100% !important; top: 0; 








                share|improve this answer



























                  up vote
                  -1
                  down vote













                  My solution with native array methods map, filter and reduce



                  const res = arr
                  // map to currency
                  .map(i => i.balance.currency)
                  // get unique currency
                  .filter((v, i, a) => a.indexOf(v) === i)
                  // map to result
                  .map((i, a) => (
                  currency: i,
                  totalAmount: calcTotalAmount(i)
                  ));

                  // calculate amount
                  function calcTotalAmount(currency)
                  return (
                  arr
                  // filter to current curency
                  .filter(i => i.balance.currency === currency)
                  // reduce to number
                  .reduce((accum, i) => accum + i.balance.amount, 0)
                  );






                  share|improve this answer




















                    Your Answer





                    StackExchange.ifUsing("editor", function ()
                    StackExchange.using("externalEditor", function ()
                    StackExchange.using("snippets", function ()
                    StackExchange.snippets.init();
                    );
                    );
                    , "code-snippets");

                    StackExchange.ready(function()
                    var channelOptions =
                    tags: "".split(" "),
                    id: "1"
                    ;
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function()
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled)
                    StackExchange.using("snippets", function()
                    createEditor();
                    );

                    else
                    createEditor();

                    );

                    function createEditor()
                    StackExchange.prepareEditor(
                    heartbeatType: 'answer',
                    convertImagesToLinks: true,
                    noModals: false,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    );



                    );













                     

                    draft saved


                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52382319%2fget-the-total-value-of-the-matching-array-elements%23new-answer', 'question_page');

                    );

                    Post as a guest






























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    3
                    down vote













                    Here is a O(n) approach of getting that output:



                    1. You first define a empty object tempObj which will be used to store the currency and totalAmount value as a object based on the currency key

                    2. Then, if this currency key is defined in the tempObj you will simply add the amount with the totalAmount for an existing object.

                    3. Else you will create a object with the amount as totalAmount, currency as key of tempObj and currency as currency of the item in forEach loop

                    4. Finally you will need to do Object.values(tempObj) so that we get the object values and ignore the keys of tempObj to get the desired result.




                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);








                    share|improve this answer
















                    • 2




                      This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                      – malbarmawi
                      2 hours ago














                    up vote
                    3
                    down vote













                    Here is a O(n) approach of getting that output:



                    1. You first define a empty object tempObj which will be used to store the currency and totalAmount value as a object based on the currency key

                    2. Then, if this currency key is defined in the tempObj you will simply add the amount with the totalAmount for an existing object.

                    3. Else you will create a object with the amount as totalAmount, currency as key of tempObj and currency as currency of the item in forEach loop

                    4. Finally you will need to do Object.values(tempObj) so that we get the object values and ignore the keys of tempObj to get the desired result.




                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);








                    share|improve this answer
















                    • 2




                      This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                      – malbarmawi
                      2 hours ago












                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Here is a O(n) approach of getting that output:



                    1. You first define a empty object tempObj which will be used to store the currency and totalAmount value as a object based on the currency key

                    2. Then, if this currency key is defined in the tempObj you will simply add the amount with the totalAmount for an existing object.

                    3. Else you will create a object with the amount as totalAmount, currency as key of tempObj and currency as currency of the item in forEach loop

                    4. Finally you will need to do Object.values(tempObj) so that we get the object values and ignore the keys of tempObj to get the desired result.




                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);








                    share|improve this answer












                    Here is a O(n) approach of getting that output:



                    1. You first define a empty object tempObj which will be used to store the currency and totalAmount value as a object based on the currency key

                    2. Then, if this currency key is defined in the tempObj you will simply add the amount with the totalAmount for an existing object.

                    3. Else you will create a object with the amount as totalAmount, currency as key of tempObj and currency as currency of the item in forEach loop

                    4. Finally you will need to do Object.values(tempObj) so that we get the object values and ignore the keys of tempObj to get the desired result.




                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);








                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);





                    var arr = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                    modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                    modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                    modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                    modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                    modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                    var tempObj = ;
                    arr.forEach((obj)=>
                    if(tempObj[obj.balance.currency])
                    tempObj[obj.balance.currency].totalAmount += obj.balance.amount
                    else
                    tempObj[obj.balance.currency] =
                    currency: obj.balance.currency,
                    totalAmount : obj.balance.amount


                    );
                    var resArray = Object.values(tempObj);
                    console.log(resArray);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    Ankit Agarwal

                    20.9k41841




                    20.9k41841







                    • 2




                      This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                      – malbarmawi
                      2 hours ago












                    • 2




                      This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                      – malbarmawi
                      2 hours ago







                    2




                    2




                    This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                    – malbarmawi
                    2 hours ago




                    This wonderful ,I was thinking the same way but I do many steps after the forEach but you mange all in single loop , I can't do more than upvote
                    – malbarmawi
                    2 hours ago












                    up vote
                    2
                    down vote













                    You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:






                    const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                    const result = Object.values(data.reduce((r, balance ) =>
                    const amount, currency = balance;
                    if(!r[currency]) r[currency] = currency, amount: 0 ;

                    r[currency].amount += amount;

                    return r;
                    , ));

                    console.log(result);








                    share|improve this answer
























                      up vote
                      2
                      down vote













                      You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:






                      const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                      const result = Object.values(data.reduce((r, balance ) =>
                      const amount, currency = balance;
                      if(!r[currency]) r[currency] = currency, amount: 0 ;

                      r[currency].amount += amount;

                      return r;
                      , ));

                      console.log(result);








                      share|improve this answer






















                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:






                        const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                        const result = Object.values(data.reduce((r, balance ) =>
                        const amount, currency = balance;
                        if(!r[currency]) r[currency] = currency, amount: 0 ;

                        r[currency].amount += amount;

                        return r;
                        , ));

                        console.log(result);








                        share|improve this answer












                        You can Array.reduce() to iterate the data. If a currency doesn't exist in the accumulator (r in the reduce callback), initialize it. Add the current amount, to the currency amount in the accumulator. Get an array of currencies using Object.values:






                        const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                        const result = Object.values(data.reduce((r, balance ) =>
                        const amount, currency = balance;
                        if(!r[currency]) r[currency] = currency, amount: 0 ;

                        r[currency].amount += amount;

                        return r;
                        , ));

                        console.log(result);








                        const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                        const result = Object.values(data.reduce((r, balance ) =>
                        const amount, currency = balance;
                        if(!r[currency]) r[currency] = currency, amount: 0 ;

                        r[currency].amount += amount;

                        return r;
                        , ));

                        console.log(result);





                        const data = ["modelNumber":"123456789","balance":"amount":1000,"currency":"EUR","modelNumber":"987654321","balance":"amount":2000,"currency":"EUR","modelNumber":"322353466","balance":"amount":1500,"currency":"GBP","modelNumber":"892347522","balance":"amount":1000,"currency":"USD","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":2500,"currency":"GBP","modelNumber":"931883113","balance":"amount":3000,"currency":"INR","modelNumber":"854300564","balance":"amount":3500,"currency":"USD"];

                        const result = Object.values(data.reduce((r, balance ) =>
                        const amount, currency = balance;
                        if(!r[currency]) r[currency] = currency, amount: 0 ;

                        r[currency].amount += amount;

                        return r;
                        , ));

                        console.log(result);






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 2 hours ago









                        Ori Drori

                        64.1k96682




                        64.1k96682




















                            up vote
                            0
                            down vote













                            It's possible to reduce the values you have and them map them into the format you require:






                            const values = [
                            modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                            modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                            modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                            modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                            modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                            modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                            modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                            modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                            ];

                            const results = values.reduce((prev, curr) => (
                            ...prev,
                            [curr.balance.currency]: (prev[curr.balance.currency] ), )

                            const inCorrectFormat = Object.keys(results).map(key => (
                            currency: key,
                            totalAmount: results[key]
                            ))

                            console.dir(inCorrectFormat)








                            share|improve this answer
























                              up vote
                              0
                              down vote













                              It's possible to reduce the values you have and them map them into the format you require:






                              const values = [
                              modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                              modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                              modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                              modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                              modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                              modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                              modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                              modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                              ];

                              const results = values.reduce((prev, curr) => (
                              ...prev,
                              [curr.balance.currency]: (prev[curr.balance.currency] ), )

                              const inCorrectFormat = Object.keys(results).map(key => (
                              currency: key,
                              totalAmount: results[key]
                              ))

                              console.dir(inCorrectFormat)








                              share|improve this answer






















                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                It's possible to reduce the values you have and them map them into the format you require:






                                const values = [
                                modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                                modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                                modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                                modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                                ];

                                const results = values.reduce((prev, curr) => (
                                ...prev,
                                [curr.balance.currency]: (prev[curr.balance.currency] ), )

                                const inCorrectFormat = Object.keys(results).map(key => (
                                currency: key,
                                totalAmount: results[key]
                                ))

                                console.dir(inCorrectFormat)








                                share|improve this answer












                                It's possible to reduce the values you have and them map them into the format you require:






                                const values = [
                                modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                                modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                                modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                                modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                                ];

                                const results = values.reduce((prev, curr) => (
                                ...prev,
                                [curr.balance.currency]: (prev[curr.balance.currency] ), )

                                const inCorrectFormat = Object.keys(results).map(key => (
                                currency: key,
                                totalAmount: results[key]
                                ))

                                console.dir(inCorrectFormat)








                                const values = [
                                modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                                modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                                modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                                modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                                ];

                                const results = values.reduce((prev, curr) => (
                                ...prev,
                                [curr.balance.currency]: (prev[curr.balance.currency] ), )

                                const inCorrectFormat = Object.keys(results).map(key => (
                                currency: key,
                                totalAmount: results[key]
                                ))

                                console.dir(inCorrectFormat)





                                const values = [
                                modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,
                                modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,
                                modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,
                                modelNumber: "892347522", balance: amount:1000, currency:"USD" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,
                                modelNumber: "931883113", balance: amount:3000, currency:"INR" ,
                                modelNumber: "854300564", balance: amount:3500, currency:"USD" ,
                                ];

                                const results = values.reduce((prev, curr) => (
                                ...prev,
                                [curr.balance.currency]: (prev[curr.balance.currency] ), )

                                const inCorrectFormat = Object.keys(results).map(key => (
                                currency: key,
                                totalAmount: results[key]
                                ))

                                console.dir(inCorrectFormat)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 2 hours ago









                                OliverRadini

                                2,013626




                                2,013626




















                                    up vote
                                    0
                                    down vote













                                    I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:






                                    let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                    let balanceArray = initialArray.map(el => el.balance);

                                    let finalObject = balanceArray.reduce((acc, el) =>
                                    if(acc[el.currency]) acc[el.currency] += el.amount
                                    else acc[el.currency] = el.amount
                                    return acc;
                                    ,);

                                    let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                    console.log(finalArray)








                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote













                                      I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:






                                      let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                      let balanceArray = initialArray.map(el => el.balance);

                                      let finalObject = balanceArray.reduce((acc, el) =>
                                      if(acc[el.currency]) acc[el.currency] += el.amount
                                      else acc[el.currency] = el.amount
                                      return acc;
                                      ,);

                                      let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                      console.log(finalArray)








                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:






                                        let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                        let balanceArray = initialArray.map(el => el.balance);

                                        let finalObject = balanceArray.reduce((acc, el) =>
                                        if(acc[el.currency]) acc[el.currency] += el.amount
                                        else acc[el.currency] = el.amount
                                        return acc;
                                        ,);

                                        let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                        console.log(finalArray)








                                        share|improve this answer












                                        I would first use map to reduce the array elements to just their balance, and then use reduce to make a new object from which I can then map a new array:






                                        let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                        let balanceArray = initialArray.map(el => el.balance);

                                        let finalObject = balanceArray.reduce((acc, el) =>
                                        if(acc[el.currency]) acc[el.currency] += el.amount
                                        else acc[el.currency] = el.amount
                                        return acc;
                                        ,);

                                        let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                        console.log(finalArray)








                                        let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                        let balanceArray = initialArray.map(el => el.balance);

                                        let finalObject = balanceArray.reduce((acc, el) =>
                                        if(acc[el.currency]) acc[el.currency] += el.amount
                                        else acc[el.currency] = el.amount
                                        return acc;
                                        ,);

                                        let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                        console.log(finalArray)





                                        let initialArray = [modelNumber: "123456789", balance: amount:1000, currency:"EUR" ,modelNumber: "987654321", balance: amount:2000, currency:"EUR" ,modelNumber: "322353466", balance: amount:1500, currency:"GBP" ,modelNumber: "892347522", balance: amount:1000, currency:"USD" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:2500, currency:"GBP" ,modelNumber: "931883113", balance: amount:3000, currency:"INR" ,modelNumber: "854300564", balance: amount:3500, currency:"USD" ];

                                        let balanceArray = initialArray.map(el => el.balance);

                                        let finalObject = balanceArray.reduce((acc, el) =>
                                        if(acc[el.currency]) acc[el.currency] += el.amount
                                        else acc[el.currency] = el.amount
                                        return acc;
                                        ,);

                                        let finalArray = Object.keys(finalObject).map(k => (currency:k, amount:finalObject[k]));


                                        console.log(finalArray)






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 2 hours ago









                                        Luca Kiebel

                                        6,91831431




                                        6,91831431




















                                            up vote
                                            0
                                            down vote













                                            You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.






                                            var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                            result = Array.from(
                                            data.reduce((m, balance: amount, currency ) =>
                                            m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                            ([currency, totalAmount]) => ( currency, totalAmount )
                                            );

                                            console.log(result);

                                            .as-console-wrapper max-height: 100% !important; top: 0; 








                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote













                                              You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.






                                              var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                              result = Array.from(
                                              data.reduce((m, balance: amount, currency ) =>
                                              m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                              ([currency, totalAmount]) => ( currency, totalAmount )
                                              );

                                              console.log(result);

                                              .as-console-wrapper max-height: 100% !important; top: 0; 








                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.






                                                var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                                result = Array.from(
                                                data.reduce((m, balance: amount, currency ) =>
                                                m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                                ([currency, totalAmount]) => ( currency, totalAmount )
                                                );

                                                console.log(result);

                                                .as-console-wrapper max-height: 100% !important; top: 0; 








                                                share|improve this answer












                                                You could take a Map and render the wanted array of currency/totalAmount pairs as new objects.






                                                var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                                result = Array.from(
                                                data.reduce((m, balance: amount, currency ) =>
                                                m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                                ([currency, totalAmount]) => ( currency, totalAmount )
                                                );

                                                console.log(result);

                                                .as-console-wrapper max-height: 100% !important; top: 0; 








                                                var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                                result = Array.from(
                                                data.reduce((m, balance: amount, currency ) =>
                                                m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                                ([currency, totalAmount]) => ( currency, totalAmount )
                                                );

                                                console.log(result);

                                                .as-console-wrapper max-height: 100% !important; top: 0; 





                                                var data = [ modelNumber: "123456789", balance: amount: 1000, currency: "EUR" , modelNumber: "987654321", balance: amount: 2000, currency: "EUR" , modelNumber: "322353466", balance: amount: 1500, currency: "GBP" , modelNumber: "892347522", balance: amount: 1000, currency: "USD" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 2500, currency: "GBP" , modelNumber: "931883113", balance: amount: 3000, currency: "INR" , modelNumber: "854300564", balance: amount: 3500, currency: "USD" ],
                                                result = Array.from(
                                                data.reduce((m, balance: amount, currency ) =>
                                                m.set(currency, (m.get(currency) || 0) + amount), new Map),
                                                ([currency, totalAmount]) => ( currency, totalAmount )
                                                );

                                                console.log(result);

                                                .as-console-wrapper max-height: 100% !important; top: 0; 






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 2 hours ago









                                                Nina Scholz

                                                159k1279139




                                                159k1279139




















                                                    up vote
                                                    -1
                                                    down vote













                                                    My solution with native array methods map, filter and reduce



                                                    const res = arr
                                                    // map to currency
                                                    .map(i => i.balance.currency)
                                                    // get unique currency
                                                    .filter((v, i, a) => a.indexOf(v) === i)
                                                    // map to result
                                                    .map((i, a) => (
                                                    currency: i,
                                                    totalAmount: calcTotalAmount(i)
                                                    ));

                                                    // calculate amount
                                                    function calcTotalAmount(currency)
                                                    return (
                                                    arr
                                                    // filter to current curency
                                                    .filter(i => i.balance.currency === currency)
                                                    // reduce to number
                                                    .reduce((accum, i) => accum + i.balance.amount, 0)
                                                    );






                                                    share|improve this answer
























                                                      up vote
                                                      -1
                                                      down vote













                                                      My solution with native array methods map, filter and reduce



                                                      const res = arr
                                                      // map to currency
                                                      .map(i => i.balance.currency)
                                                      // get unique currency
                                                      .filter((v, i, a) => a.indexOf(v) === i)
                                                      // map to result
                                                      .map((i, a) => (
                                                      currency: i,
                                                      totalAmount: calcTotalAmount(i)
                                                      ));

                                                      // calculate amount
                                                      function calcTotalAmount(currency)
                                                      return (
                                                      arr
                                                      // filter to current curency
                                                      .filter(i => i.balance.currency === currency)
                                                      // reduce to number
                                                      .reduce((accum, i) => accum + i.balance.amount, 0)
                                                      );






                                                      share|improve this answer






















                                                        up vote
                                                        -1
                                                        down vote










                                                        up vote
                                                        -1
                                                        down vote









                                                        My solution with native array methods map, filter and reduce



                                                        const res = arr
                                                        // map to currency
                                                        .map(i => i.balance.currency)
                                                        // get unique currency
                                                        .filter((v, i, a) => a.indexOf(v) === i)
                                                        // map to result
                                                        .map((i, a) => (
                                                        currency: i,
                                                        totalAmount: calcTotalAmount(i)
                                                        ));

                                                        // calculate amount
                                                        function calcTotalAmount(currency)
                                                        return (
                                                        arr
                                                        // filter to current curency
                                                        .filter(i => i.balance.currency === currency)
                                                        // reduce to number
                                                        .reduce((accum, i) => accum + i.balance.amount, 0)
                                                        );






                                                        share|improve this answer












                                                        My solution with native array methods map, filter and reduce



                                                        const res = arr
                                                        // map to currency
                                                        .map(i => i.balance.currency)
                                                        // get unique currency
                                                        .filter((v, i, a) => a.indexOf(v) === i)
                                                        // map to result
                                                        .map((i, a) => (
                                                        currency: i,
                                                        totalAmount: calcTotalAmount(i)
                                                        ));

                                                        // calculate amount
                                                        function calcTotalAmount(currency)
                                                        return (
                                                        arr
                                                        // filter to current curency
                                                        .filter(i => i.balance.currency === currency)
                                                        // reduce to number
                                                        .reduce((accum, i) => accum + i.balance.amount, 0)
                                                        );







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 2 hours ago









                                                        Kliment Ru

                                                        3376




                                                        3376



























                                                             

                                                            draft saved


                                                            draft discarded















































                                                             


                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52382319%2fget-the-total-value-of-the-matching-array-elements%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest













































































                                                            Comments

                                                            Popular posts from this blog

                                                            What does second last employer means? [closed]

                                                            List of Gilmore Girls characters

                                                            Confectionery