-4

I have an array of objects as:

  0: {id: "1", name: "Tab1", address: "123 Street"}
  1: {id: "2", name: "Tab2", address: "456 Avenue"}
  2: {id: "3", name: "Tab3", address: "789 st"}

I want to grab a particular object from the above array based on the "name" key. For example, if I pass the key as "Tab1" it should return me:

0: {id: "1", name: "Tab1", address: "123 Street"}

I can loop through the array and get the values but wanted to know is there any simpler/efficient way to get the desired data.

v-gael
  • 126
  • 9
kaka1234
  • 770
  • 3
  • 9
  • 30

1 Answers1

-1

You can use Array.prototype.find like this

const searchArray = key => array.find(({name})=> {
    return name === key;
});

console.log(searchArray("Tab1"));
Teneff
  • 30,564
  • 13
  • 72
  • 103
  • 4
    By answering this question, you are engorging users not to do an research by they ask, it is clear that this question shows no research efforts of any kind – Hyyan Abo Fakher Jul 19 '18 at 14:54