Here is the exercise
I’m going through this part of the question:
Use the track’s id
property to check if the current song is in the playlistTracks
state.
The solution uses .find()
to go through the array and look for an element with an id
that matches the parameter track id
:
let tracks = this.state.playlistTracks;
if (tracks.find( t => t.id === track.id )) {
return
}
tracks.push(track);
this.setState({
playlistTracks: tracks
})
}
My understanding is that .find()
returns the first element in the array that matches the condition given, t.id === track.id
Does returning an element satisfy the if statement? I thought that what you put into the if statement had to evaluate to true or false, and that not returning true would mean that the track
would always be added to playlistTracks
.