lorstevens
12/12/2019 - 8:45 PM

Reduce array of objects

const blogs = [
  { title: "Not so popular", comments: [ "Its ok...I guess"]},
  { title: "Popular...ish", comments: [ "Sweet", "Tell me more"]},
  { title: "Super popular", comments: [ "This is amazing, thanks!", 
                                       "Wow, I never thought about it this way", 
                                       "Great stuff!"]
  }
]

const comments = blogs.reduce((accumulator, blog) => {
  return accumulator.concat(blog.comments)
}, [])

// returns ["Its ok...I guess","Sweet","Tell me more","This is amazing, thanks!",
// "Wow, I never thought about it this way","Great stuff!"]