0

What is the correct way to loop through each 'repo'?

 ...
render(){
    const watchers = [];
    const names = [];

    this.props.repos.map(repo => (
        names.push(repo.name)
        watchers.push(repo.watchers) 
    ));
    return(...)
}
2
  • 1
    What is the wrong with this? Just add undefined check this.props.repos && this.props.repos.map Commented May 20, 2018 at 9:31
  • Instead map use forEach Commented May 20, 2018 at 10:04

1 Answer 1

1

I think the easiest way to achieve this would be

...
render(){
    const watchers = this.props.repos.map(r => r.watchers);
    const names = this.props.repos.map(r => name);
    return(...)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why there is a need to loop two time.?What is the wrong with above?

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.