javascript - jQuery access object in loop -


i trying basic loop on object getting json file:

// in json file , loop on data need chart $.each(jsonresponse.data.escalationssubmittedbylocation.dataset, function() {      tempdata.push(array(this.loc, parseint(this.total))) }) 

when debug , check data looping over, showing correct object:

enter image description here

the issue inside loop , try refer object using this.loc or this.total cannot find it.

enter image description here

in picture above, hovering on line 676 data object looping over. can see, object contains both total , loc.

however, when @ line debugging, this referring values string or something.

any idea why can't access values in object looping on when object exists?

additional details:

// when called, render charts based on json file created         function dorender() {              $.getjson(jsonfile, function(data) {                  jsonresponse = data;                 tempdata.length = 0;                  // in json file , loop on data need chart                 $.each(jsonresponse.data.escalationtypes.dataset, function() {                     tempdata.push(array(this.reason, parseint(this.total)))                 }) ... chart uses array data here ... 

this should work (and works in jsfiddle) :

obj = {"total" : "foo", "loc" : "bar"}; tempdata = []; temploop = [];     $.each(obj, function(index, value) {         temploop[index] = value;         if (index === "loc") {             tempdata.push(temploop);         }     }) console.log(tempdata); 

the callback function in $.each have index , value parameters , need these pass keys , values in loop. loop process array element every iteration, need build final array want get.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -