Javascript 如何获取对象数组中对象的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4755005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the index of an object inside an array of objects?
提问by VerizonW
I have a JavaScript array of objects like this:
我有一个 JavaScript 对象数组,如下所示:
box[0] = {...}
box[1] = {...}
box[2] = {...}
...
box[499] = {...}
This objects are generated by the same constructor and added to the array inside a loop. The objects have methods in the prototype which need to know the object's indexin the array to do their stuff. Currently what I am doing is to set a property called id
inside each object when I create it inside the loop, equal to the array index. Something like this:
此对象由相同的构造函数生成并添加到循环内的数组中。对象在原型中有方法,需要知道对象在数组中的索引才能完成它们的工作。目前我正在做的是id
在循环内创建每个对象时设置一个在每个对象内调用的属性,等于数组索引。像这样的东西:
box[i].id = i;
However I am not totally satisfied with this because each time I reorder the array using sort()
I have to run a loop to update the id
properties with the new index values.
但是我对此并不完全满意,因为每次我使用重新排序数组时,sort()
我都必须运行一个循环来id
使用新的索引值更新属性。
My question is if there is a way to know inside an object its indexin the array, without having to set the id property, hope you can help me.
我的问题是,是否有一种方法可以知道对象内部在数组中的索引,而无需设置 id 属性,希望您能帮助我。
Thanks in advance.
提前致谢。
回答by user113716
I don't think a function inside an object in the Array is going to be aware of the index of the Array that is referencing it.
我认为 Array 中对象内的函数不会知道引用它的 Array 的索引。
Because each item in the Array is simply pointing to that object in memory, there could conceivably be dozens of Array items, variables, Object properties, etc that reference that same object, so the function (or the object that contains the function) wouldn't know which back reference you're hoping to make.
因为数组中的每一项都只是指向内存中的那个对象,所以可以想象有几十个数组项、变量、对象属性等引用同一个对象,所以函数(或包含函数的对象)不会不知道您希望进行哪个反向引用。
I'm guessing you'll be stuck doing what you're doing if you need to know its index in the Array.
如果您需要知道它在 Array 中的索引,我猜您会被困在正在做的事情上。
I suppose that function could call indexOf()
against the Array since it returns an index
, but that would require some overhead for each call, and you'll need to added it for unsupported browsers.
我想该函数可以调用indexOf()
Array,因为它返回一个index
,但是每次调用都需要一些开销,并且您需要为不受支持的浏览器添加它。
theArr.indexOf( this ); // assuming the function was called from the context
// of the object in question
回答by German Attanasio
Why don't you add a unique property on each object and use that property to lookup the indexOf that Object.
为什么不在每个对象上添加一个唯一的属性并使用该属性来查找该对象的 indexOf。
If you have a constructor you can add an _id and then lookup that id using:
如果您有一个构造函数,您可以添加一个 _id,然后使用以下方法查找该 id:
function getIndex(box, objectId) {
var index = box.map(function(e) { return e._id; }).indexOf(objectId);
return index;
}
This will work even if you sort the array, but you need to keep the id unique.
即使您对数组进行排序,这也会起作用,但您需要保持 id 唯一。
Array.prototype.map
is not available on IE7 or IE8. ES5 Compatibility
Array.prototype.map
在 IE7 或 IE8 上不可用。ES5 兼容性
btw, I don't like this solution but it will work on your problem :)
顺便说一句,我不喜欢这个解决方案,但它可以解决您的问题:)