+ 1

Reduce get a object in an array with reduce

How do you get a object out of a array with reduce? So if the element that contains the object object is a property name with value "rendang," then pick up the entire object and store it on the mysel variable. So how can I do that with reduce? https://code.sololearn.com/Wo39IUiAqqkw/?ref=app

1st Mar 2020, 8:53 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
5 Antworten
+ 1
Mirielle👽 I was thinking that, too, but I was just trying to see if the reduce could do that, too
1st Mar 2020, 9:09 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
+ 1
with reduce ? reduce((p, n)=> (!p && n.name == "Rendang")? n: p, null) imo filter is the best way, the intent are much clearer by using filter instead of reduce.
1st Mar 2020, 10:22 PM
Taste
Taste - avatar
0
try this: var mysel = myData.filter(obj => obj.name == "Rendang") console.log(mysel)
1st Mar 2020, 9:24 PM
Gianni
0
Thank you so much everyone!! It was solved
1st Mar 2020, 11:33 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar