遍历数组并返回同一级别的子节点

时间:2022-12-30 20:21:32

I have the following array structure:

我有以下数组结构:

rgInventory": {

"2085630349": {
    "id": "2085630349",
    "classid": "253266389",
    "instanceid": "253354499",
    "amount": "1",
    "pos": 1
},
"1938126110": {
    "id": "1938126110",
    "classid": "57939745",
    "instanceid": "0",
    "amount": "1",
    "pos": 2
}, 
...

I need to be able to identify which child of rgInventory has the property pos = 1, and then return the id of this child array (in the example above, this would be 2085630349.

我需要能够识别rgInventory的哪个子项具有属性pos = 1,然后返回此子数组的id(在上面的示例中,这将是2085630349。

Thanks!

谢谢!

1 个解决方案

#1


1  

What you have is not an array, just an object.

你拥有的不是一个数组,只是一个对象。

You can loop through the keys of an object using a for-in loop, like this:

您可以使用for-in循环遍历对象的键,如下所示:

var key;
for (key in obj) {
    // `key` will be each key, in no particular order
}

It loops not only through the object's own properties, but through enumerable properties it inherits from its prototype. Simple objects like the one you have there don't inherit any enumerable properties from their prototype (unless someone does something really silly, below), but if you wanted to be sure, you could use if (obj.hasOwnProperty(key)) in the loop above to only handle its own and not inherited ones.

它不仅通过对象自己的属性循环,而且通过它的原型继承的可枚举属性循环。像你那里的简单对象不会从他们的原型继承任何可枚举的属性(除非有人在下面做了一些非常愚蠢的事情),但如果你想确定,你可以使用if(obj.hasOwnProperty(key))in上面的循环只处理自己的而不是继承的。

Then just look at obj[key], which is a reference to the child object, and see if it has the relevant pos property.

然后只需查看obj [key],它是对子对象的引用,并查看它是否具有相关的pos属性。


Just for completeness, the really silly thing would be this:

只是为了完整,真正愚蠢的事情是:

Object.prototype.someName = /* some value */;

...which would create an enumerable property on the prototype used for simple objects. This should never be done.

...将在用于简单对象的原型上创建一个可枚举属性。永远不应该这样做。

#1


1  

What you have is not an array, just an object.

你拥有的不是一个数组,只是一个对象。

You can loop through the keys of an object using a for-in loop, like this:

您可以使用for-in循环遍历对象的键,如下所示:

var key;
for (key in obj) {
    // `key` will be each key, in no particular order
}

It loops not only through the object's own properties, but through enumerable properties it inherits from its prototype. Simple objects like the one you have there don't inherit any enumerable properties from their prototype (unless someone does something really silly, below), but if you wanted to be sure, you could use if (obj.hasOwnProperty(key)) in the loop above to only handle its own and not inherited ones.

它不仅通过对象自己的属性循环,而且通过它的原型继承的可枚举属性循环。像你那里的简单对象不会从他们的原型继承任何可枚举的属性(除非有人在下面做了一些非常愚蠢的事情),但如果你想确定,你可以使用if(obj.hasOwnProperty(key))in上面的循环只处理自己的而不是继承的。

Then just look at obj[key], which is a reference to the child object, and see if it has the relevant pos property.

然后只需查看obj [key],它是对子对象的引用,并查看它是否具有相关的pos属性。


Just for completeness, the really silly thing would be this:

只是为了完整,真正愚蠢的事情是:

Object.prototype.someName = /* some value */;

...which would create an enumerable property on the prototype used for simple objects. This should never be done.

...将在用于简单对象的原型上创建一个可枚举属性。永远不应该这样做。