how can i get data by using filter between array?

i have Comment and ReplyComments data. I want to get only the data whose Comment data id and CommentId of ReplyComments match by using filter method. and i want to put data in arr2

answer it would be

         arr2 =
           [
            0: {id: 62, CommentId: 28}
            1: {id: 64, CommentId: 28}
            ]

this is data

            Comment = [
            0: {id: 28, content: "2", createdAt: "2021-03-07T14:07:19.000Z", updatedAt: "2021-03-07T14:07:19.000Z", UserId: 1, …}
            1: {id: 29, content: "3", createdAt: "2021-03-07T14:08:28.000Z", updatedAt: "2021-03-07T14:08:28.000Z", UserId: 1, …}
            2: {id: 30, content: "4", createdAt: "2021-03-07T14:25:15.000Z", updatedAt: "2021-03-07T14:25:15.000Z", UserId: 1, …}
            ]


            ReplyComments = [
            0: {id: 62, CommentId: 28}
            1: {id: 64, CommentId: 28}
            ]

i already tried but it doesn’t work how can i fix my code??

this is my code

    let arr2 = []

    const arr = Comment.map((v) => {
        arr2 = ReplyComments.filter((value) => v.id === value.CommentId)
      
    })

    console.log("arr2:",arr2);

Answer

This is almost good. Try this :

const arr2 = ReplyComments.filter(reply => Comment.find(comment => reply.CommentId === comment.id));

console.log(arr2);

Leave a Reply

Your email address will not be published. Required fields are marked *