Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda