我有一个包含数字数据的数组,我需要定位数组内对象的值。
举个例子来说明这一点,这里是数据:
[
{1: 'one'},
{2: 'two'},
{3: 'three}
]
我正在减少它以获得我想要的,所以我只剩下[{3: '三'}]
。
现在如何获取“三”字符串?
非常感谢
最佳答案
Any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime).
var obj = [{
3: 'three'
}];
console.log(obj[0][3]);
如果 key 未知:
var obj = [{
3: 'three'
}];
console.log(obj[0][Object.keys(obj[0])]);
关于javascript - 来自单个数组的目标对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39777123/