我有一系列像这样的对象 const arrayOne = [ { id: 'call_1', callDuration: 3 }, { id: 'call_2', callDuration: 5 }, { id: 'call_3', callDuration: 4 }, { id: 'call_4', callDuration: 8 }, { id: 'call_5', callDuration: 3 }, { id: 'call_6', callDuration: 4 }, { id: 'call_7', callDuration: 6 }, { id: 'call_8', callDuration: 7 }, { id: 'call_9', callDuration: 25 }, { id: 'call_10', callDuration: 25 },];我需要从 arrayOne 中创建以下一个: const arrayTwo = [ [ { id: 'call_1', callDuration: 3 }, { id: 'call_2', callDuration: 5 }, ], [ { id: 'call_2', callDuration: 5 }, { id: 'call_3', callDuration: 4 }, ], [ { id: 'call_3', callDuration: 4 }, { id: 'call_4', callDuration: 8 }, ], [ { id: 'call_4', callDuration: 8 }, { id: 'call_5', callDuration: 3 }, ], [ { id: 'call_5', callDuration: 3 }, { id: 'call_6', callDuration: 4 }, ], [ { id: 'call_6', callDuration: 4 }, { id: 'call_7', callDuration: 6 }, ], [ { id: 'call_7', callDuration: 6 }, { id: 'call_8', callDuration: 7 }, ], [ { id: 'call_8', callDuration: 7 }, { id: 'call_9', callDuration: 25 }, ], [ { id: 'call_9', callDuration: 25 }, { id: 'call_10', callDuration: 25 }, ],];我想要的基本上是创建一个包含对象的新数组。我不知道如何处理这个问题。如果您有任何想法,请告诉我。
1 回答
UYOU
TA贡献1878条经验 获得超4个赞
你可以这样做
const arrayTwo = [];
for(let i=0; (i+1) < arrayOne.length; i+=1) {
const temp = [ arrayOne[i], arrayOne[i+1] ];
arrayTwo.push(temp);
}
添加回答
举报
0/150
提交
取消