配列要素をオブジェクト配列につづり合わせる
940 ワード
テーマ:次の配列arr 1、arr 2があります.まずarr 1とarr 2の要素をarr 3の形につなぎ合わせてください.
var arr1=[1000,2000,3000];
var arr2=[1,2,3];
var arr3 = [{"name":"1000","value","1"},{"name":"2000","value","2"},{"name":"3000","value","3"}];
方法:
var arr1=[1000,2000,3000];
var arr2=[1,2,3];
var arr3 = [];
var arr32 = [];
arr1.forEach(function(item,index){
arr3.push({
value: arr2[index],
name: arr1[index]
})
});
console.log(arr3);
arr32 = arr1.map(function(item,index){
return {
name: item,
value: arr2[index]
}
});
console.log(arr32);
var arr33 = "[";
for(var i=0;i<arr1.length;i++){
arr33 += "{'value':"+arr2[i]+",'name':"+arr1[i]+"},";
}
arr33 = arr33.substring(0,arr33.lastIndexOf(','));
arr33 += "]";
arr33 = eval("("+arr33+")");
console.log(arr33)