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

194
Views
Javascript: agregar objetos a la matriz con una identificación coincidente

Tengo una matriz de categories que tiene el id de valor que estoy tratando de hacer coincidir con mis articles de matriz

Si los articles tienen una identificación coincidente con una de las categories que me gustaría combinarlos en una matriz. Puedo agregar estos objetos a la matriz, pero no coinciden según la identificación.

Aquí hay un ejemplo de categorías:

 const categories = [{results: { "id": 28, "name": "Articles" }, { "id": 76, "name": "Projects" } }]

Aquí hay un ejemplo de artículos:

 const articles = [ { "title": "first article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "second article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "thirdarticle", "content": "lorem ipsum", "categoryId": 76 }, ]

Estoy intentando combinar las dos matrices en una. Si es posible, me gustaría que la matriz de articles sea una matriz en ese índice coincidente. Por ejemplo:

 const combined = [ { "id": 28, "name": "Articles", "articles:: [ { "title": "first article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "second article", "content": "lorem ipsum", "categoryId": 28 }, ], }, ];

Intenté hacer esto mapeando categorías y usando Object.assign . Los resultados de salida parecen combinar matrices, pero creo que todas están combinadas y no coinciden con la identificación.

¿Cómo puedo lograr esto?

 const articles = [ { "title": "first article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "second article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "thirdarticle", "content": "lorem ipsum", "categoryId": 76 }, ] const categories = {results: [{ "id": 28, "name": "Articles"}, {"id": 76, "name": "Projects"}]} let combined = categories.results.map((item, i ) => Object.assign(item, articles)); console.log(combined)

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

0

Aquí hay una forma de llegar usando su código pero filtrando artículos coincidentes y usando el operador de spread

 let combined = categories.results.map(item => ({ ...item, articles: articles.filter(f => f.categoryId == item.id) })); 

 const articles = [{ "title": "first article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "second article", "content": "lorem ipsum", "categoryId": 28 }, { "title": "thirdarticle", "content": "lorem ipsum", "categoryId": 76 }, ] const categories = { results: [{ "id": 28, "name": "Articles" }, { "id": 76, "name": "Projects" }] } let combined = categories.results.map(item => ({ ...item, articles: articles.filter(f => f.categoryId == item.id) })); console.log(combined)

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!