如何将两个数组转换为对象?
我的阵列:
arr1.push(3,2,3,6,11,7);
arr2.push("num1","num2","num3","num4","num5","num6");
如何将其转换为对象:
[{name:"num1", value:3}, {name:"num2", value:2}, {name:"num3", value:3}, {name:"num4", value:6}, {name:"num5", value:11}, {name:"num6", value:7}]
答案 0 :(得分:3)
你可以尝试
var obj = [];
for(var i = 0; i < arr1.length; i++){
obj[i] = {name : arr1[i], value: arr2[i]};
}
答案 1 :(得分:0)
var arr = [],
cv = -1;
while(++cv < arr1.length) {
arr.push({name:arr2[cv], value:arr1[cv]});
}
答案 2 :(得分:0)
你基本上必须合并两个数组。假设两个数组总是具有相等的长度
arr1.push(3,2,3,6,11,7);
arr2.push("num1","num2","num3","num4","num5","num6");
var obj = [], i;
for( i=0; i<l; i++) {
obj.push(arr1[i]+":"+arr2[i]);
}
// obj is now required object