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
  • 35,261
  • 9
  • 79
  • 93
Hamid Shoja
  • 2,340
  • 3
  • 24
  • 32

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
  • 16,417
  • 3
  • 24
  • 44