gpt4 book ai didi

javascript - 来自单个数组的目标对象值

转载 作者:行者123 更新时间:2023-11-28 15:04:23 24 4
gpt4 key购买 nike

我有一个包含数字数据的数组,我需要定位数组内对象的值。

举个例子来说明这一点,这里是数据:

[
{1: 'one'},
{2: 'two'},
{3: 'three}
]

我正在减少它以获得我想要的,所以我只剩下[{3: '三'}]。现在如何获取“三”字符串?

非常感谢

最佳答案

使用bracket notation

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com