Home > Software engineering >  concat objects values inside arrays inside another array with the same property
concat objects values inside arrays inside another array with the same property

Time:03-11

[
  [
    {
      pointInTime: "2/28/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/1/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/2/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/3/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/4/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/5/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/6/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/7/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/8/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/9/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/10/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/11/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/12/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/13/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/14/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/15/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/16/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/17/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/18/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/19/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/20/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/21/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/22/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/23/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/24/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/25/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/26/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/27/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/28/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/29/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/30/2022",
      result: {
        price: 48,
        priceLevel: 2,
      },
    },
  ],
  [
    {
      pointInTime: "2/28/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/1/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/2/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/3/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/4/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/5/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/6/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/7/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/8/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/9/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/10/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/11/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/12/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/13/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/14/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/15/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/16/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/17/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/18/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/19/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/20/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/21/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/22/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/23/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/24/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/25/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/26/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/27/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/28/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/29/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/30/2022",
      result: {
        price: 83,
        priceLevel: 2,
      },
    },
  ],
  [
    {
      pointInTime: "2/28/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/1/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/2/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/3/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/4/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/5/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/6/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/7/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/8/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/9/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/10/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/11/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/12/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/13/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/14/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/15/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/16/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/17/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/18/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/19/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/20/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/21/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/22/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/23/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/24/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/25/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/26/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/27/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/28/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/29/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/30/2022",
      result: {
        price: 79,
        priceLevel: 2,
      },
    },
  ],
  [
    {
      pointInTime: "2/28/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/1/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/2/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/3/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/4/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/5/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/6/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/7/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/8/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/9/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/10/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/11/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/12/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/13/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/14/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/15/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/16/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/17/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/18/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/19/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/20/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/21/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/22/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/23/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/24/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/25/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/26/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/27/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/28/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/29/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/30/2022",
      result: {
        price: 40,
        priceLevel: 2,
      },
    },
  ],
  [
    {
      pointInTime: "2/28/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/1/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/2/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/3/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/4/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/5/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/6/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/7/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/8/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/9/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/10/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/11/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/12/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/13/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/14/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/15/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/16/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/17/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/18/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/19/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/20/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/21/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/22/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/23/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/24/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/25/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/26/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/27/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/28/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/29/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
    {
      pointInTime: "3/30/2022",
      result: {
        price: 67,
        priceLevel: 2,
      },
    },
  ],
]

I have this array which is holding multiple arrays that hold multiple objects inside them, and as you can see some of the objects have the same pointInTime value, and I want to cobine the objects that have the same pointInTime and concatenate the price and priceLevel with them, like so:

[
    [
       {
        pointInTime: "2/28/2022",
        result: {
        price: 48 - 83,
        priceLevel: 2 - 2,
                },
       }
    ]
]

I have used lodash's unionBy method, but that only gets the unique ones, I want the similar ones to be mixed up together like I have shown.

CodePudding user response:

You could collect all values in array grouped by pointInTime.

const
    data = [[{ pointInTime: "2/28/2022", result: { price: 48, priceLevel: 2 } }, { pointInTime: "3/1/2022", result: { price: 48, priceLevel: 2 } }], [{ pointInTime: "2/28/2022", result: { price: 83, priceLevel: 2 } }, { pointInTime: "3/1/2022", result: { price: 83, priceLevel: 2 } }], [{ pointInTime: "2/28/2022", result: { price: 79, priceLevel: 2 } }, { pointInTime: "3/1/2022", result: { price: 79, priceLevel: 2 } }], [{ pointInTime: "2/28/2022", result: { price: 40, priceLevel: 2 } }, { pointInTime: "3/1/2022", result: { price: 40, priceLevel: 2 } }], [{ pointInTime: "2/28/2022", result: { price: 67, priceLevel: 2 } }, { pointInTime: "3/1/2022", result: { price: 67, priceLevel: 2 } }]],
    result = Object.values(data.flat().reduce((r, { result: { price, priceLevel }, ...o }) => {
        r[o.pointInTime] ??= { ...o, result: { price: [], priceLevel: [] } };
        r[o.pointInTime].result.price.push(price);
        r[o.pointInTime].result.priceLevel.push(priceLevel);
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related