0

There is a string variable that has a path like /app/something/xx/4/profile besides there is an array of string like **

const arr=[
     {name:'xx',etc...}, 
     {name:'yy',etc...},
     {name:'zz',etc...}
    ]

I want to find the first index of array that the string variable has the name in simplest way.

wentjun
  • 40,384
  • 10
  • 95
  • 107
Hamid Shoja
  • 3,838
  • 4
  • 30
  • 45

1 Answers1

2

Use Array.findIndex which :

returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

const str = "/app/something/xx/4/profile";

const arr = [{ name: "xx" }, { name: "yy" }, { name: "zz" }];

const index = arr.findIndex(e => str.includes(e.name));

console.log({ index });
Taki
  • 17,320
  • 4
  • 26
  • 47