[英]How to access array within an object within an array?
I have both an object and an array: 我既有对象又有数组:
var elementsArr = [];
var elements = {
polygon: 734,
infoContent: "huhu",
id: 0
}
I am going to have multiple "elements" and push each one into "elementsArr". 我将有多个“元素”,并将每个元素推入“ elementsArr”。
for(var i=0; i<20; i++){
elements.id = id +=1;
elementsArr.push(elements);
}
My problem now is how to access a specific "id" within the object elements, within the array elementsArr and pushing this into another array. 我的问题现在是如何访问对象元素中,数组elementsArr中的特定“ id”并将其推入另一个数组中。
I tried this but it doesn't seem to be working: 我试过了,但似乎没有用:
var ids = [];
for(var m=0; m<elementsArr.length; m++){
if(elements.id == elementsArr[m]){
ids.push(elements.id);
}
How do I do this? 我该怎么做呢?
Your code is pushing the same object onto the array over and over again. 您的代码将相同的对象一遍又一遍地推到阵列上。
One way to fix that would be to write a function to get a new element: 解决该问题的一种方法是编写一个函数以获取新元素:
function Element(id) {
return {
polygon: 734,
infoContent: "huhu",
id: id
};
}
for(var i=0; i<20; i++){
elementsArr.push(Element(i));
}
If there are going to be a lot of elements, and the "id" values are all relatively small numbers, you'd be better off storing the elements such that the "id" is also the index: 如果会有很多元素,并且“ id”值都相对较小,那么最好存储这些元素,以使“ id”也是索引:
for (var i = 0; i < 20; i++)
elementsArr[i] = Element(i);
To get the element with "id" 17, then: 要获得具有“ id” 17的元素,则:
var element17 = elementsArr[17];
If you really want to search, however, you can use .find()
: 但是,如果您确实要搜索,则可以使用
.find()
:
var element17 = elementsArr.find(function(elem) { return elem.id === 17; });
or a simple loop: 或一个简单的循环:
for (var i = 0; i < elementsArr.length; ++i) {
if (elementsArr[i].id === 17) {
// found it!
}
}
You can extract the "id" values from the array with a simple call to .map()
: 您可以通过简单调用
.map()
从数组中提取“ id”值:
var ids = elementsArr.map(function(elem) { return elem.id; });
or another for
loop: 或另一个
for
循环:
var ids = [];
for (var i = 0; i < elementsArr.length; ++i)
ids.push(elementsArr[i].id);
There are a couple of ways to do this. 有两种方法可以做到这一点。 If you are able to work in
ES6
then a WeakMap
would be great. 如果您能够在
ES6
工作,那么WeakMap
会很棒。
However, I'm going to give you an ES5
instead. 但是,我将改为为您提供
ES5
。
Option 1 Loop through the data. 选项1遍历数据。
If you aren't accessing the data often, then looping through as you've done so may be the best choice. 如果您不经常访问数据,那么循环访问可能是最好的选择。
Option 2 Set up your own index 选项2设置自己的索引
On the other hand, if you need faster lookup, you can set up your own separate index to look things up. 另一方面,如果需要更快的查找,则可以设置自己的单独索引来查找内容。
var elementsLookup = {}; // Create an empty object.
for(var i=0; i<20; i++){
elements.id = id +=1;
elementsLookup[id] = elements; // Stash off a copy
elementsArr.push(elements);
}
Then you can look things up with a 然后,您可以使用
var elem = elementsLookup[2]; // Get the element with an id of 2.
It is easy to do it with Array prototyping. 使用数组原型很容易做到。 Here is a function
findById
这是一个功能
findById
Array.prototype.findById = function(id){
for(var x = 0 ; x < this.length; x++){
if(this[x].id == id){
return this[x];
}
}
return false;
}
You can use this function to recieve any id from the array or false if it does not exists 您可以使用此函数从数组中接收任何ID;如果不存在,则返回false
elementsArr.findById(17);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.