5

I have an Array of Objects called comments, and I'm trying to select only one's that have the id of post that I need to another array of objects. And the problem is that I can't find a way to copy the object I have found. This is my function:

comments = [];
commentspart = [];
private loadPartComments(id){          
            this.comments.forEach(element => {
            if (element.postId == id) {
                this.commentspart = ????;
                }
            });
            return this.commentspart;
        }

Thank you.

1
  • How about: this.commentspart = element;? Commented Aug 31, 2016 at 17:59

1 Answer 1

7

i guess you are looking for filter,

    comments = [];
commentspart = [];
private loadPartComments(id){          
            this.commentspart = this.comments.filter(element => {
               return element.postId == id;
            });
        }

it will give you filtered array of comments based upon id.

Hope this helps!!

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.