Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Javascript arrays into one array

I have an array articles that has arrays results. I am attempting to combine all these results into one array without the results for example:

const articles = [{ "id": 203, "title": "testing"}, {"id": 213,"title": "new title"}, { "id": 1, "title": "one"}, {"id": 2,"title": "two"}, { "id": 32, "title": "test article"}, {"id": 62,"title": "title test"}]

I've attempted to achieve this by mapping articles but the return result are still separate arrays instead of 1 array of objects. How can I achieve this?

Here is my code snippet:

const articles = [{results: [{ "id": 203, "title": "testing"}, {"id": 213,"title": "new title"}]}, {results: [{ "id": 1, "title": "one"}, {"id": 2,"title": "two"}]}, {results: [{"id": 62,"title": "title test"}]} ]



let mappedArticles = articles.map(article => {
    return article.results
})
console.log(mappedArticles)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use flatMap

const articles = [{results: [{ "id": 203, "title": "testing"}, {"id": 213,"title": "new title"}]}, {results: [{ "id": 1, "title": "one"}, {"id": 2,"title": "two"}]}, {results: [{"id": 62,"title": "title test"}]} ]

let mappedArticles = articles.flatMap(article => article.results)
console.log(mappedArticles)

about 4 years ago · Juan Pablo Isaza Report

0

In case flatMap is not supported by your environment, there is another way to achieve it, although a bit more verbose and non-trivial, which uses the concat method of the Array prototype:

const articles = [{results: [{ "id": 203, "title": "testing"}, {"id": 213,"title": "new title"}]}, {results: [{ "id": 1, "title": "one"}, {"id": 2,"title": "two"}]}, {results: [{"id": 62,"title": "title test"}]} ]

const mappedArticles = articles.reduce((acc, article) => acc.concat(article.results), []);

console.log(mappedArticles);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!