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
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
Share Improve this question edited Jan 26, 2022 at 5:21 Harry Wardana asked Jan 26, 2022 at 4:47 Harry WardanaHarry Wardana 2771 gold badge5 silver badges15 bronze badges 4-
Loop the array, and grab the object property from the array index for example
myLokasi[0].id
would be valid, obviously many ways to achieve the desired result. a loop is just one. – MysticSeagull Commented Jan 26, 2022 at 4:50 - can give an example? – Harry Wardana Commented Jan 26, 2022 at 4:52
-
[2,3,4,3].map(id => mylokasi.find(e => e.id == id).name)
also I want to mention thatqontol==penis
in indonesia language idk why this guy use it as name example – buncis Commented Jan 26, 2022 at 5:04 - let arrr = mylokasi.map((item) => item.name) gives you new array like ['koko', 'doni', 'dika', 'qotol'] – Ozal Zarbaliyev Commented Jan 26, 2022 at 5:05
2 Answers
Reset to default 5You can use the filter and map functions:
mylocasi
.filter(item => [2, 3, 4, 3].includes(item.id))
.map(item => item.name)
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');