I have an array like this:
const array = [
{
id: "1",
value: "alpha"
},
{
id: "2",
value: "beta"
},
{
id: "3",
value: "beta"
},
{
id: "4",
value: "omega"
}
];
I need a method that returns only the first object that meets my criteria in this case the attribute value.
I wrote this method with a loop:
findObject(array, value) {
for(let i = 0; i < array.length; i++) {
if(array[i].value === value) {
return i;
}
}
return -1;
}
Is there any way to do it without the loop? Thank you.