0

Can i use .every() on an array of objects and how?

18th Mar 2021, 3:10 AM
Darío Estevanez
Darío Estevanez - avatar
3 Respuestas
+ 2
Yes, you can use every() on anything that's an array. So, you could use it like this on an object's array: const popSongs = ["Umbrella", "Happy", "Marry You", "Thriller", I Wanna Dance With Somebody"] const person = { firstName: "Jane", favouriteSongs: [ {name: "Thriller"}, {name: "Don't Know Why"} ] } const lovesOnlyPopSongs = person.favouriteSongs.every(song => popSongs.includes(song.name)) The function passed to the every function will return false for one of the songs in the person's favouriteSongs array. This means the lovesOnlyPopSongs variable will contain false.
18th Mar 2021, 4:24 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
Darío Estevanez I've changed my answer to use an array of objects. I first read your question as can every() be used on an object's array. Notice now how favouriteSongs is an array of objects. The object property we're interested in (song.name) is passed to the includes() function.
19th Mar 2021, 1:05 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
CamelBeatsSnake thank you so much, but now i have another related question 😅 https://www.sololearn.com/Discuss/2730521/?ref=app
19th Mar 2021, 8:45 PM
Darío Estevanez
Darío Estevanez - avatar