是否有任何简短而简洁的方法可以在以下列表中找到具有给定searchObject规范的项目?
var list = [
{ id:1, name: "foo", description: "description 1" },
{ id:2, name: "bar", description: "description 2" },
{ id:3, name: "baz", description: "description 3" },
];
var searchObject = { id: 2, name: "bar" };
searchObject不一定具有列表中项目的所有属性。我需要属性之间的运算符为AND (id==2 && name=="bar")。以下是我想出的。javascript中是否有任何本机可以做到这一点?
for (var i in list) {
var found = true;
for (var p in searchObject) {
if (list[i][p] !== searchObject[p]) {
found = false;
break;
}
}
if(found) return list[i];
}