答案 0 :(得分:3)
var a = [1,2,3,4,5,6,7,8,9,10,11,12]
var m = 3
var n = 2
var required = a.map( (val, idx) => [val, Math.floor(idx/(m*n))])
也许你想要这个
答案 1 :(得分:2)
您可以使用m
和n
值的除法结果映射值。
var data = [1, 3, 7, 2, 5, 9, 4, 32],
m = 3,
n = 2,
result = data.map(function (a, i) {
return [a, Math.floor(i / (m * n))];
});
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)
您也可以使用array#reduce
。
var array = [1,3,7,2,5,9,4,32],
m = 3,
n = 2;
var result = array.reduce((res, v, i) => {
res.push([v,Math.floor(i/(m*n))]);
return res
} , []);
console.log(result);
答案 3 :(得分:1)
从图像中看起来好像是要将项目[1,3,7,2,5,9,4,32]
隔离为嵌套数组[[1,3,7,2,5,9],[4,32]]
在这种情况下,请尝试此操作(评论内联)
var output=[[]], m = 3, n = 2; //initialize the output
[1,3,7,2,5,9,4,32].forEach( function(item, index){ //iterate the array
if ( ( index % (m*n) == 0) && index > 0)
{
output.push([]); // if index value reaches the multiple of m*n, add new []
}
output[ output.length - 1 ].push( item );
});
console.log(JSON.stringify(output))