1

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" }
1
  • your questions is a bit confusing but try using Array.prototype.find if you are willing to find an object (the first one that match the predicate) data in an array Commented Mar 15, 2021 at 8:05

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.