javascript - How do I refer to a variable stored in an array instead of its value? -
i have set of variables represent prices , number of items sold in 2d array. have sorted in order find lowest price.
i'd set second variable (number sold) of first item (player a) value (200) referring array.
for example:
var playerasold; var arr=[ [playeraprice,playerasold], [playerbprice,playerbsold], [playercprice,playercsold]]; arr[0][1]=200;
this doesn't work, because playerasold
has value of 0 , trying set 0=0.
how refer variable , not value of variable?
javascript has no notion of c's pointers or c++'s references, you'll have in different way. rather trying store references in array, try making array sole holder of data.
might this:
var players = [ { price: 5, sold: 1 }, { price: 3, sold: 6 }, { price: 9, sold: 2 } ];
then rather than, say, playerbsold
, can use players[1].sold
. can use variable in place of 1
if wanted.
Comments
Post a Comment