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 index in 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 index in the array, without having to set the id property, hope you can help me.
我的问题是,如果有一种方法可以知道一个对象的数组中的索引,而不需要设置id属性,希望您能帮助我。
Thanks in advance.
提前谢谢。
2 个解决方案
#1
19
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.
我不认为数组中对象内部的函数会知道引用它的数组的索引。
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.
我猜如果你需要知道它在数组中的索引,你会被困住做你正在做的事情。
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(),因为它返回一个索引,但是这将需要每次调用的一些开销,并且您需要为不受支持的浏览器添加它。
theArr.indexOf( this ); // assuming the function was called from the context
// of the object in question
#2
2
Why don't you add a unique property on each object and use that property to lookup the indexOf that Object.
为什么不在每个对象上添加一个惟一属性并使用该属性查找该对象的索引呢?
If you have a constructor you can add an _id and then lookup that id using:
如果您有一个构造函数,您可以添加一个_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。地图不能在IE7或IE8上使用。ES5的兼容性
btw, I don't like this solution but it will work on your problem :)
顺便说一句,我不喜欢这个解决方案,但它可以解决你的问题:)
#1
19
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.
我不认为数组中对象内部的函数会知道引用它的数组的索引。
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.
我猜如果你需要知道它在数组中的索引,你会被困住做你正在做的事情。
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(),因为它返回一个索引,但是这将需要每次调用的一些开销,并且您需要为不受支持的浏览器添加它。
theArr.indexOf( this ); // assuming the function was called from the context
// of the object in question
#2
2
Why don't you add a unique property on each object and use that property to lookup the indexOf that Object.
为什么不在每个对象上添加一个惟一属性并使用该属性查找该对象的索引呢?
If you have a constructor you can add an _id and then lookup that id using:
如果您有一个构造函数,您可以添加一个_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。地图不能在IE7或IE8上使用。ES5的兼容性
btw, I don't like this solution but it will work on your problem :)
顺便说一句,我不喜欢这个解决方案,但它可以解决你的问题:)