I have a javascript code like this:
var myData=[];
$.getJSON( path_url , function(data){
var len = data.rows.length;
for (var i = 0; i < len; i++){
var code = data.rows[i].codeid;
var color = data.rows[i].color;
var obj = {}
obj[code] = color
myData.push(obj);
}
console.log(myData);
});
and the results
0: Object{12: "#fc0516"},1: Object{14: "#17a030"},2: Object{31: "#17a030"}
but the results I would like this:
{12:"#fc0516",14:"#17a030",31:"#17a030"}
how can I get the result as above? sorry for my bad english, thanks in advance...
Create myData
as an object, then use bracket notation to assign the property values
var myData = {};
$.getJSON(path_url, function (data) {
var len = data.rows.length;
for (var i = 0; i < len; i++) {
var code = data.rows[i].codeid;
var color = data.rows[i].color;
myData[code] = color
}
console.log(myData);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.