I am trying to filter duplicates from two arrays...
responseData.posts and fbFormattedPosts are both arrays of post objects. Some posts appear in both arrays with the same 'postId'. I'd like to set my state to an array containing all posts of responseData.posts... then I want to add the fbFormattedPosts to that array - But I don't want to include the fbFormattedPosts that have the same 'postId'.
I set my 'posts' useState array using the useState function setPosts. If I use the spread operator for them, they both add all the posts from each array. This works, but I end up with duplicate posts.
//set All Posts
setPosts([ ...responseData.posts, ...fbFormattedPosts])
So, I am trying to compare the Ids to filter them out.
setPosts([ ...responseData.posts, fbFormattedPosts.forEach(fbPost => {
responseData.posts.filter(resPost => {
if(fbPost.postId !== resPost.postId ){
return fbPost
}
})
})])
Obviously this doesn't work... Now I get no posts at all. Not even the responseData.posts.