Heim > Fragen und Antworten > Hauptteil
怪我咯2017-04-11 13:01:55
//指定插入
function pushdArray(arr, index, value) { //arr 被插二维数组 index二维数组索引 value插入值
if(index >= arr.length) {
return;
}
arr[index].push(value);
return arr;
}
var tArr = [[1,2,3],["a","b","c"],["A","B","C"]];;
console.log(JSON.stringify(pushdArray(tArr, 0, 123)));
//[[1,2,3,123],["a","b","c"],["A","B","C"]]
遍历循环插入不同值,就加个循环,没传的值就不push。逻辑视业务而定
function pushdArray(arr, valueArr) {
if(valueArr.length) {
for(var i = 0; i < arr.length; i++) {
if(valueArr[i]) {
arr[i].push(valueArr[i]);
}
}
}
return arr;
}
var tArr = [[1,2,3],["a","b","c"],["A","B","C"]];
var vArr = [4,'d','D'];
console.log(JSON.stringify(pushdArray(tArr, vArr)));
//[[1,2,3,4],["a","b","c","d"],["A","B","C","D"]]
PHP中文网2017-04-11 13:01:55
JS 可以定义二维数组吗?
JS 只有数组,但数组元素可以也是数组,以此实现二维数组,但是定义不出来。
然后剩下的就是对数组,以及数组元素(另一个数组)的操作了。
大家讲道理2017-04-11 13:01:55
aa=[[1,2,3],[1,2,3],[1,2,3]]
[Array[3], Array[3], Array[3]]
aa[1]
[1, 2, 3]
aa[1].push(4)
4
aa[1]
[1, 2, 3, 4]
aa
[Array[3], Array[4], Array[3]]
就酱紫~