1. 程式人生 > >陣列轉集合

陣列轉集合

var activeSubjectsName = ["語文", "數學", "英語", "思想品德", "科學"];
 var activeSubjectsNum = [46, 2, 2, 28, 29];
 var activeSubjectsArr = [];
 
for (var i = 0; i < activeSubjectsName.length; i++) {
 console.log(i);
 var activeSubjectsObject = {};
 for (var j = 0; j < activeSubjectsNum.length; j++) {
  if (i == j) {
   activeSubjectsObject.name = activeSubjectsName[i];
   activeSubjectsObject.value = activeSubjectsNum[j];
   activeSubjectsArr.push(activeSubjectsObject);
  }
 }
}
console.log(activeSubjectsArr);
// activeSubjectsArr=[
// {name: "語文", value: 46},
// {name: "數學", value: 2},
// {name: "英語", value: 2},
// {name: "思想品德", value: 28},
// {name: "科學", value: 29}
// ]