我有一个对象数组:
0: {id: "1", name: "Tab1", address: "123 Street"}
1: {id: "2", name: "Tab2", address: "456 Avenue"}
2: {id: "3", name: "Tab3", address: "789 st"}
我想基于“名称”键从上述数组中获取特定对象。例如,如果我将密钥作为“ Tab1”传递,它将返回我:
0: {id: "1", name: "Tab1", address: "123 Street"}
我可以遍历数组并获取值,但想知道是否有任何更简单/有效的方法来获取所需数据。
答案 0 :(得分:-1)
您可以像这样使用Array.prototype.find
const searchArray = key => array.find(({name})=> {
return name === key;
});
console.log(searchArray("Tab1"));