在具有相同 属性 的另一个数组中连接数组中的对象值
concat objects values inside arrays inside another array with the same property
[
[
{
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,
},
},
],
]
我有这个数组,它包含多个数组,其中包含多个对象,正如您所看到的,一些对象具有相同的 pointInTime 值,我想合并具有相同 pointInTime 的对象并连接price 和 priceLevel,像这样:
[
[
{
pointInTime: "2/28/2022",
result: {
price: 48 - 83,
priceLevel: 2 - 2,
},
}
]
]
我使用了 lodash 的 unionBy
方法,但只能得到唯一的方法,我希望将相似的方法混合在一起,就像我展示的那样。
您可以收集按 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; }
[
[
{
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,
},
},
],
]
我有这个数组,它包含多个数组,其中包含多个对象,正如您所看到的,一些对象具有相同的 pointInTime 值,我想合并具有相同 pointInTime 的对象并连接price 和 priceLevel,像这样:
[
[
{
pointInTime: "2/28/2022",
result: {
price: 48 - 83,
priceLevel: 2 - 2,
},
}
]
]
我使用了 lodash 的 unionBy
方法,但只能得到唯一的方法,我希望将相似的方法混合在一起,就像我展示的那样。
您可以收集按 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; }