Angular: I have a variable, myNumber = 5
and an array of objects, ids and names.
nameArray: Nameset[] = [];
The array looks like this:
0: {id: 1, name: "Jim"}
1: {id: 3, name: "Bob"}
2: {id: 5, name: "Carl"}
What is the best way to the the matching name from the array where id === myNumber
I always use a forEach loop like this, but is this the best way?
getName(myNumber) {
let helper = '';
this.nameArray.forEach(aNameset => {
if (aNameset.id === myNumber) {
helper = aNameset.name;
}
})
return helper;
}