I want to find data by value, but the value is an array .. how?
mylokasi [
{
id: 1 ,
name : koko
},
{
id: 2 ,
name : doni
},
{
id: 3 ,
name : dika
},
{
id: 4 ,
name : ujang
},
]
mylokasi.find(p => p.id== [2,3,4,3]).name
I want to display all the data in the array [2,3,4,3]
result value so : doni,dika,ujang,dika
CodePudding user response:
You can use the filter and map functions:
mylocasi
.filter(item => [2, 3, 4, 3].includes(item.id))
.map(item => item.name)
CodePudding user response:
Using map() and find() you can achieve your goal !
Try this code it's help you !
let mylokasi = [{ id: 1, name: 'koko' }, { id: 2, name: 'doni' }, { id: 3, name: 'dika' }, { id: 4, name: 'qotol' }];
let arr = [2, 3, 4, 3];
let res = arr.map((id) => (mylokasi.find(x => x.id == id).name));
console.log(res, 'res');
