I want to iterate using forEach
and, when the correct element's been located, return the value. I was under impression that a simple return
inside the forEach
would suffice but learned that the result in the calling statement was undefined
.
So I had to declare an output variable scoped to the whole of the method and tick through all the elements, despite the fact that the right one's already been found. (The elements are unique.)
getMenuIndex(url) {
let output = -1;
this.menus.forEach(app => {
app.subs.forEach(sub => {
console.log(sub.link + " vs " + url);
if (sub.link === url)
// return sub.id;
output = sub.id;
});
});
return output;
}
This is code smell long way. Is there a neater way to pick the ID of the element matching the URL condition?