gpt4 book ai didi

javascript - 从深层属性中选择对象数组的项目

转载 作者:行者123 更新时间:2023-12-02 16:48:52 25 4
gpt4 key购买 nike

假设我有这个数组:

var myArray = [
{
"name": "item 1",
"id": 123
},{
"name": "item 2",
"id": 456
}
];

根据 id 属性获取项目的正确方法是什么?在此示例中,假设我知道我想要 item.id == 456item

我不需要 jquery 答案。

最佳答案

试试这个:

var item = myArray.filter(function(item) { return item.id === 456; })[0];

关于javascript - 从深层属性中选择对象数组的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864044/

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