简体   繁体   English

在不知道关键的情况下从对象获取价值

[英]Get value from object without knowing the key

I have item.124bn1353v = 'foo' 我有item.124bn1353v = 'foo'

However the 124bn1353v key is dynamically generated but I need to get its value foo . 但是, 124bn1353v密钥是动态生成的,但是我需要获取其值foo How can I get the foo assuming that we have only one property in the item ? 假设item只有一个属性,如何获取foo

If there is only one property set on the item object this will assign it to the variable foo : 如果在item对象上仅设置了一个属性,则将其分配给变量foo

var foo;
for (var prop in item) {
    if (item.hasOwnProperty(prop)) {
        foo = item[prop];
    }
}

Example JSFiddle - http://jsfiddle.net/VDyUe/ 示例JSFiddle- http://jsfiddle.net/VDyUe/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM