在Js中,如何将两个list遍历拼接为一个list3=[{a:1},{b:2},{c:3},{d:4}]。

2024-11-16 01:33:16
推荐回答(1个)
回答(1):

function arrcat(arr1,arr2){
if(!Array.isArray(arr1) || !Array.isArray(arr2) || arr1.length != arr2.length) return [];
var arr = [];
for(var i = 0,len = arr1.length;i var obj = new Object();
obj[arr1[i]] = arr2[i];
arr[i]=obj;
}
return arr;
}
arrcat(["a","b","c","d"],[1,2,3,4]);//结果:[{a:1},{b:2},{c:3},{d:4}]