把二維陣列轉換為json字串
var data = [ ['選項1', 10], ['選項2', 10], ['選項3', 50],['選項4', 30] ]; var oneArr=[].concat.apply([],data);//["選項1", 35, "選項2", 15, "選項3", 25, "選項4", 25] var jsonArr=[]; var nameArr=[]; for(var i=0;i<oneArr.length;i++){ if(i%2==0){ var dataName=oneArr[i]; var str='{name:"'+oneArr[i]+'",value:'+oneArr[i+1]+'}';
var jsonStr=eval('('+str+')');
nameArr.push(dataName);jsonArr.push(jsonStr);
}
}
console.log(jsonArr);//[{name: "選項1", value: 10},{name: "選項2", value: 10},{name: "選項3", value: 50},{name: "選項4", value: 30}]
console.log(nameArr);//["選項1", "選項2", "選項3", "選項4"]