gpt4 book ai didi

javascript - 使用javascript在对象数组中查找值

转载 作者:行者123 更新时间:2023-11-30 07:21:26 24 4
gpt4 key购买 nike

鉴于以下情况:

var myArray = [
{
id: "3283267",
innerArray: ["434","6565","343","3665"]
},
{
id: "9747439",
innerArray: ["3434","38493","4308403840","34343"]
},
{
id: "0849374",
innerArray: ["343434","57575","389843","38493"]
}
];

我如何搜索 myArray 中的对象以确定字符串“38493”是否存在于 innerArray 中,然后返回一个包含对象 ID 的新数组....像这样:

var arrayWithIds = ["9747439", "0849374"];

最佳答案

使用 Array.forEachArray.indexOf 函数的简单解决方案:

var search =  "38493", 
result = [];

myArray.forEach(function(o) {
if (o.innerArray.indexOf(search) !== -1) this.push(o.id);
}, result);

console.log(result); // ["9747439", "0849374"]

关于javascript - 使用javascript在对象数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516829/

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