You have a couple of options.
First and foremost, findIndex
. You pass it a function that tests if an element is what you are looking for, it returns the index of the first element that makes that function return true
.
x.findIndex((o) => o.id === 'roadshows');
const x = [{
"id": "roadshows",
"name": "Roadshows"
}, {
"id": "sporting_events",
"name": "Sporting Events"
}];
console.log(x.findIndex((o) => o.id === 'roadshows'));
Another option is first mapping the relevant property to an array and searching in that one.
x.map((o) => o.id).indexOf('roadshows');
const x = [{
"id": "roadshows",
"name": "Roadshows"
}, {
"id": "sporting_events",
"name": "Sporting Events"
}];
console.log(x.map((o) => o.id).indexOf('roadshows'));