I'm trying to remove the duplicates from an array.map function in React and I'm not sure why I'm getting a "Cannot read property of undefined" error.
The below code does not return any results, which is what I expected because video.project.id is equal to videos[i].project.id:
this.props.videos.map((video, i, videos) => {
if (video.project.id !== videos[i].project.id) {
return (
<option
key={video.id}
value={video.project.id}
>
{video.project.projectName}
</option>
)
}
})
So I would think the below code should return one of each entry but I instead get the error:
this.props.videos.map((video, i, videos) => {
if (video.project.id !== videos[i - 1].project.id) {
return (
<option
key={video.id}
value={video.project.id}
>
{video.project.projectName}
</option>
)
}
})
Can anyone tell me what I'm missing here? Or is there a better way to filter this?
0,0-1=-1->videos[-1] == undefinedvideo, i, videosifblock doesn't trigger. If this happens you will getundefined