How can I reduce elements in object?

Jack23

I have an object like this:

result: 
     > rows:
       > 0: {key: Array(4), value: 3}
          > key: (4) ["Person", "2020-06-24", "Product, "00000000008"]
            value: 3
       > 1: {key: Array(4), value: 10}
          > key: (4) ["Person", "2020-06-25", "Product, "00000000009"]
            value: 10
       > 2: {key: Array(4), value: 10}
          > key: (4) ["Person", "2020-06-25", "Product, "00000000008"]
            value: 10

Now, what I need to do is to reduce this result checking for the same code (for example 00000000008) and sum the value, to obtain:

(for example)

00000000008   value: 13

Now, my problem is how to do, I have tried to use first a map and then a reduce, but I don't understand how can I check for the same code and sum the value.

How can I do?

I have tried in this way, but it doesn't work:

res is the object with the values

let example = res.rows.map((element)=> {
        console.log("ELEMENT IS ", element)
        let example1 = element.key[3].reduce(function(element, v){
          if(ref.hasOwnProperty(v))
            element[ref[v]] += v;
          else {
            ref[v] = element.length;
            element.push(prev = v)
          }
          return element
        }, [])
      })
      console.log("element", element)
DDomen

The Array.map method is useful for data transformations, but if you have to aggregate is mostly expensive because you have also to Array.filter the non-aggregated values.

You can use Array.reduce (MDN) instead in order to build your own object:

let result = {
  rows: [
    {
      key: ["Person", "2020-06-24", "Product", "00000000008"],
      value: 3
    },
    {
      key: ["Person", "2020-06-25", "Product", "00000000009"],
      value: 10
    },
    {
      key: ["Person", "2020-06-25", "Product", "00000000008"],
      value: 10
    }
  ]
}

let output1 = result.rows.reduce((acc, current) => {
  let key = current.key[3];
  // adding value to the accumulator
  acc[key] = (acc[key] || 0) + current.value;
  return acc;
}, {});

console.log(output1);

let output2 = result.rows.reduce((acc, current) => {
  // check if key is already present
  let found = acc.find(v => v.key == current.key[3])
  // if it is, update the current value
  if (found) {
    found.value += current.value;
  }
  // otherwise create a new one
  else {
    acc.push({ key: current.key[3], value: current.value });
  }
  return acc;
}, []);

console.log(output2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I reduce the space between two elements in CSS?

From Dev

How can I reduce array with non-unique elements by summing their second value with lodash/underscore?

From Dev

How many elements can I store in a HashMap object in Java

From Dev

How can I pass a Polymer Object beetwen 2 custom elements

From Dev

How can I reduce the size of my repository?

From Dev

How can I reduce the duplicated use of if and else

From Dev

How can I reduce the size of the shapes in visio?

From Dev

How can I reduce this long list of if statements?

From Java

how can i reduce repeated code in laravel?

From Dev

How can I reduce boilerplate from this function?

From Dev

How can I reduce startup time for vim?

From Dev

How can I improve this sum with reduce method

From Dev

How can I reduce code duplication in this JavaScript?

From Dev

How can I reduce the size of a response to an HttpWebRequest

From Dev

How can I reduce the volume of DenyHosts emails?

From Dev

How can I implement partial reduce in scala?

From Dev

How can I reduce code duplication in this JavaScript?

From Dev

How can I reduce the number of TTYs?

From Dev

How can I reduce the duplicated use of if and else

From Dev

How can I reduce boilerplate from this function?

From Dev

How can I reduce the width of an area?

From Dev

How can I reduce the consumption of the `vmmem` process?

From Dev

How can I reduce the complexity for this case?

From Dev

how can i reduce the execution time for this code

From Dev

How can I reduce whitespace in Python plot?

From Dev

How can I reduce the size of a library folder?

From Dev

How can I reduce the execution time of a view?

From Dev

Can I use HTML elements as Object keys?

From Dev

Can I search the elements of an object in an array?

Related Related

  1. 1

    How can I reduce the space between two elements in CSS?

  2. 2

    How can I reduce array with non-unique elements by summing their second value with lodash/underscore?

  3. 3

    How many elements can I store in a HashMap object in Java

  4. 4

    How can I pass a Polymer Object beetwen 2 custom elements

  5. 5

    How can I reduce the size of my repository?

  6. 6

    How can I reduce the duplicated use of if and else

  7. 7

    How can I reduce the size of the shapes in visio?

  8. 8

    How can I reduce this long list of if statements?

  9. 9

    how can i reduce repeated code in laravel?

  10. 10

    How can I reduce boilerplate from this function?

  11. 11

    How can I reduce startup time for vim?

  12. 12

    How can I improve this sum with reduce method

  13. 13

    How can I reduce code duplication in this JavaScript?

  14. 14

    How can I reduce the size of a response to an HttpWebRequest

  15. 15

    How can I reduce the volume of DenyHosts emails?

  16. 16

    How can I implement partial reduce in scala?

  17. 17

    How can I reduce code duplication in this JavaScript?

  18. 18

    How can I reduce the number of TTYs?

  19. 19

    How can I reduce the duplicated use of if and else

  20. 20

    How can I reduce boilerplate from this function?

  21. 21

    How can I reduce the width of an area?

  22. 22

    How can I reduce the consumption of the `vmmem` process?

  23. 23

    How can I reduce the complexity for this case?

  24. 24

    how can i reduce the execution time for this code

  25. 25

    How can I reduce whitespace in Python plot?

  26. 26

    How can I reduce the size of a library folder?

  27. 27

    How can I reduce the execution time of a view?

  28. 28

    Can I use HTML elements as Object keys?

  29. 29

    Can I search the elements of an object in an array?

HotTag

Archive