我有这三个数组:
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 }
];
时间,音符和力度的起始数组长度将始终不同,因此部分长度应与前三个中的最小值相匹配。
非常感谢任何建议,谢谢。
答案 0 :(得分:1)
你是说这个吗?
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));
答案 1 :(得分:0)
像这样:
addEventListener('load', function(){
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];
for(var i=0,part=[],l=time.length; i<l; i++){
part.push({time:time[i], note:note[i], velocity:velocity[i]});
}
console.log(part);
});
答案 2 :(得分:0)
您可以映射数组的所需部分。
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) => timeArr
.slice(0, Math.min(time.length, note.length, velocity.length))
.map((time, i) => ({
time,
note: noteArr[i],
velocity: velocityArr[i]
}));
console.log(merge(time, note, velocity));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;