gpt4 book ai didi

javascript - 将多个数组插入有序的对象数组中

转载 作者:行者123 更新时间:2023-12-03 03:18:06 25 4
gpt4 key购买 nike

我有这三个数组:

const time = ["8n", "8n", "4n", "4n", "8n", "8n"];    
const note = [422, 303, 482, 419, 478, 467, 317, 343];
const velocity = [0.57, 0.28, 0.35, 0.45, 0, 0.4, 0.53, 0.46, 0.48, 0.39];

如何将它们变成这样,注意匹配的索引:

const part = [
{ time: "8n", note: 422, velocity: 0.57 },
{ time: "8n", note: 303, velocity: 0.28 },
{ time: "4n", note: 482, velocity: 0.35 }
];

时间、音符和速度的开始数组长度总是不同的,因此部分长度应该与开始三个中的最小者匹配。

如有任何建议,我们将不胜感激,谢谢。

最佳答案

你是这个意思吗?

const time = ["8n", "8n", "4n", "4n", "8n", "8n"];
const note = [422, 303, 482, 419, 478, 467, 317, 343];
const velocity = [0.57, 0.28, 0.35, 0.45, 0, 0.4, 0.53, 0.46, 0.48, 0.39];

const merge = (timeArr, noteArr, velocityArr) => {
const length = Math.min(time.length, note.length, velocity.length);
const ret = [];

for (let i = 0; i < length; i++) {
ret.push({
time: timeArr[i],
note: noteArr[i],
velocity: velocityArr[i]
});
}

return ret;
};

console.log(merge(time, note, velocity));

关于javascript - 将多个数组插入有序的对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com