现象:
var data = { a:'123', b: '456', c: '789'}; var dataArr = []; var dataObj = {}; for(var i in data){ dataObj.name= data[i]; dataArr.push(dataObj); } console.log(dataArr); // [{name: "789"},{name: "789"},{name: "789"}]
解决方法:把对象放到循环里面
var data = { a:'123', b: '456', c: '789'}; var dataArr = []; for(var i in data){ var dataObj = {};//在里面定义对象 dataObj.name= data[i]; dataArr.push(dataObj); } console.log(dataArr); // [{name: "123"},{name: "456"},{name: "789"}]
本文作者为gengboxb,转载请注明。