I got array of objects
const countryList = [
{ name: 'Afghanistan', id: 'AF' },
{ name: 'Åland Islands', id: 'AX' },
{ name: 'Albania', id: 'AL' },
{ name: 'Algeria', id: 'DZ' }]
I want to filter the array by object "id" and get name
Here is what I have already done and it is working
getName = (id) => {
let name=[]
for (var i = 0; i < countryList.length ; i++) {
if (countryList[i].id === id) {
name.push(countryList[i]);
}
}
console.log(name[0].name)
}
Is there any better way of doing it?