Array doesnt Update in JavaScript -
i have code. need see in array y objects assigned inside statement (increase , decrease). in first console.log can see array updated in second it's empty.
function myfunction() { var y = new array(2); $.getjson('../docs/natures.json', function(data) { var nature = document.getelementbyid("nature").value; for( var = 0; i< data.natures.length; i++) { var x = data.natures[i]; if((nature.localecompare(x.name)) == 0) { y=[x.increase]; console.log(y[0]); y.push(x.decrease); console.log(y[1]); break; } } console.log(y); <!- first -> }); console.log(y); <!- second -> }
my json file :
{"natures":[ { "name":"some name", "increase": "some increase", "decrease": "some decrease" }, {...} ]}
jquery.getjson asynchronous, callback function passed run when request complete.
console.log(y); <!- second ->
occurs prior that
Comments
Post a Comment