鉴于以下情况:
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.forEach
和 Array.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/