1

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...

0

1 Answer 1

3

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

@insecti4art if you want to thank him for the answer you should mark it as the accepted answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.