我有一个 javascript 值数组 [A,B,C,D,E.....]
,我需要使用以下语法进行拆分:
[[A,B],[C,D],[E,F]]
依此类推(始终是一对)。
所以它应该是一个返回字符串的循环。
我尝试这样做,这几乎是我想要的:
text = '['+array[0]+','+array[1]+']';
for (index = 2; index < array.length; index++) {
text += '['+array[index]+','+array[index+1]+']';
console.log(text);
}
我明白
[10:00,15:45][18:30,20:00]
[10:00,15:45][18:30,20:00][20:00,undefined]
相反,我只需要得到这个:
[10:00,15:45][18:30,20:00]
最佳答案
可以尝试使文本成为一个实际的数组并插入对,就像这样
var orig=['A','B','C','D','E','F'];
var text=[];
for (index = 0; index < orig.length-1; index+=2) {
text.push([orig[index],orig[index+1]]);
console.log(JSON.stringify(text));
}
关于javascript - 如何将数组值分成两部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187132/