[英]How can I use an element from an object in array in Javascript
I have an arrayList with objects. 我有一个对象的arrayList。 And those objects have random numbers.
这些对象具有随机数。 I want to use some of them after, but I don't know how I can get to it.
我想在以后使用其中的一些,但是我不知道该如何使用。 here's my code :
这是我的代码:
for(var l=0; l<20; l++){
o.x=Math.random();
o.y=Math.random();
tab3[l]=[o.x,o.y];
}
So I want to use for example ox from the first object in the array (tab[0]). 因此,我想使用例如数组(tab [0])中第一个对象中的ox。 How is that possible?
那怎么可能? and if I wish to add some more elements, like oz to tab[0], how can I do that ?
如果我想在tab [0]中添加更多元素(如oz),该怎么做?
why do you need to put ox and oy in an array? 为什么需要将ox和oy放在一个数组中? the way you currently have it you could also call tab[0][0].x and tab[0][1].y but that doesn't make any sense to do it that way.
以您当前拥有的方式,您也可以调用tab [0] [0] .x和tab [0] [1] .y,但是那样做没有任何意义。
you could do it like this 你可以这样
for(var l=0; l<20; l++){
tab3[l]=[Math.random(),Math.random()];
}
and you would call it like tab3[0][0] or tab3[0][1] 并且您将其命名为tab3 [0] [0]或tab3 [0] [1]
or since i don't know what your variable o is you could also do 或者因为我不知道你的变量是什么,你也可以做
for(var l=0; l<20; l++){
o.x=Math.random();
o.y=Math.random();
tab3[l]=[o];
}
then you would call tab[0].x or tab[0].y 那么您将调用tab [0] .x或tab [0] .y
when you want to add z you would add 当您想添加z时,您将添加
tab[0].z = Math.random();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.