It is necessary to add an object with the properties x=100 , y=500 to the array. But for some reason, nothing happens, what is the error?
var coord_arr = []; coord_arr[0].x = 100; coord_arr[0].y = 500; alert( coord_arr[0].y ); It is necessary to add an object with the properties x=100 , y=500 to the array. But for some reason, nothing happens, what is the error?
var coord_arr = []; coord_arr[0].x = 100; coord_arr[0].y = 500; alert( coord_arr[0].y ); Try this
var coord_arr = []; coord_arr.push({x:100, y:500}); alert( coord_arr[0].y ); I will add a little answer @ akrasnov87. .push properly used to fill exactly the array. But there are still objects in JS that are often used as associative arrays. Such correctly filled through the index:
var coord_arr = {}; coord_arr[0] = {x:100, y:500}; alert( coord_arr[0].y ); coord_arr['left_eye'] = {x:200, y:-500}; alert( coord_arr['left_eye'].y ); Object.keys(coord_arr).forEach(function(key){ alert(key+" "+coord_arr[key].x); }); Source: https://ru.stackoverflow.com/questions/575333/
All Articles
arr[0]) if the array is still empty? Add something (in the sense of the object), then contact us. - user207618