Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda