Tengo una matriz de articles que tiene matrices de results . Estoy intentando combinar todos estos resultados en una matriz sin los results por ejemplo:
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"}] Intenté lograr esto asignando articles pero el resultado devuelto sigue siendo matrices separadas en lugar de 1 matriz de objetos. ¿Cómo puedo conseguir esto?
Aquí está mi fragmento de código:
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)Puedes usar mapa plano
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)En caso de que flatMap no sea compatible con su entorno, hay otra forma de lograrlo, aunque un poco más detallada y no trivial, que utiliza el método concat del prototipo Array:
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);