0

I have and array like:

let ARRAY = [
    {P1: A, P2: 1, P3: 1.1, P4: G, val: 10},
    {P1: A, P2: 1, P3: 1.2, P4: G, val: 10},
    {P1: B, P2: 1, P3: 1.1, P4: G, val: 10},
    {P1: B, P2: 1, P3: 1.2, P4: G, val: 10},
    {P1: C, P2: 1, P3: 1.1, P4: G, val: 10},
    {P1: C, P2: 1, P3: 1.2, P4: G, val: 10},
];

and I would like to obtain a less element array like:

grouping criteria: same value of P1 and P4

New array = [
    {P1: A, P2: 1, P3: "", P4: G, val: 20},
    {P1: B, P2: 1, P3: "", P4: G, val: 20},
    {P1: C, P2: 1, P3: "", P4: G, val: 20},
];

P3 has null value because the P3 value is not the same for the grouped elements

or other case grouping criteria: same value of P3

New array = [
    {P1: "", P2: 1, P3: 1.1, P4: G, val: 30},
    {P1: "", P2: 1, P3: 1.2, P4: G, val: 30},
];

any tips? thanks

I've tried to define a "key" (like key=P1+P4) to group elements and to use the 'reduce' function but I don't know how to properly use it.

Dario84
  • 1
  • 1

0 Answers0