user15322469
user15322469

Reputation: 909

how can i get object data from array by using filter or find?

i have a two data. one is array and the other is object like this

I want to put the value that matches the ReplyCommentId value of the replycomment object and the id value of the ReplyComments array as an object in selectreplycomment.

            replycomment: 
            {
            CommentId: 67
            PostId: 2
            ReplyCommentId: 32
            User: {id: 3, nickname: "스티브잡스"}
            UserId: 3
            active: "1"
            content: "11"
            createdAt: "2021-03-14T11:27:57.000Z"
            id: 96
            updatedAt: "2021-03-15T07:35:20.000Z"
            }

how can i do that?

            ReplyComments : [

            0: {id: 30, CommentId: 66}
            1: {id: 32, CommentId: 67}
            2: {id: 33, CommentId: 78}
            3: {id: 34, CommentId: 78}
            4: {id: 35, CommentId: null}
            5: {id: 36, CommentId: null}
            6: {id: 37, CommentId: null}
            7: {id: 43, CommentId: 66}
                            ]

expected value

            selectreplycomment :

            { CommentId: 67
            PostId: 2
            ReplyCommentId: 32
            User: {id: 3, nickname: "스티브잡스"}
            UserId: 3
            active: "1"
            content: "11"
            createdAt: "2021-03-14T11:27:57.000Z"
            id: 96
            updatedAt: "2021-03-15T07:35:20.000Z" }

Upvotes: 1

Views: 34

Answers (1)

Tim
Tim

Reputation: 10719

You can use the filter function and search for the same CommentId.

const replycomment = {
  CommentId: 67,
  PostId: 2,
  ReplyCommentId: 32,
  User: {id: 3, nickname: "스티브잡스"},
  UserId: 3,
  active: "1",
  content: "11",
  createdAt: "2021-03-14T11:27:57.000Z",
  id: 96,
  updatedAt: "2021-03-15T07:35:20.000Z",
}; 
            
const ReplyComments = [
  {id: 30, CommentId: 66},
  {id: 32, CommentId: 67},
  {id: 33, CommentId: 78},
  {id: 34, CommentId: 78},
  {id: 35, CommentId: null},
  {id: 36, CommentId: null},
  {id: 37, CommentId: null},
  {id: 43, CommentId: 66},
];                          
const res = ReplyComments.filter((e) => e.CommentId == replycomment.CommentId);
console.log('res', res);

Upvotes: 1

Related Questions