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

125
Views
Combinar 3 matrices haciendo coincidir ID

Tengo un pequeño ejercicio, para fusionar tres matrices en una matriz haciendo coincidir las ID, me gustaría agregar la matriz de Me gusta y comentarios en la matriz de publicaciones, ¿cómo haría esto?

 const posts = [ { _id: 1 , message: 'this is post one' , likes: [ { id: 1111 } , { id: 2222 } ] , comments: [ { id: 3333 } , { id: 4444 } ] } ] const comments = [ { id: 3333, message: 'this is comment 1' } , { id: 4444, message: 'this is comment 2' } ] const likes = [ { id: 1111, user: 'Peter' } , { id: 2222, user: 'John' } ]

Rendimiento esperado:

 newArray = [ { _id: 1 , message: 'this is post one' , likes: [ { id: 1111, user: 'Peter'} , { id: 2222, user: 'John'} ] , comments: [ { id: 3333, message: 'this is comment 1'} , { id: 4444, message: 'this is comment 2'} ] } ]

Aquí hay un enfoque similar que he usado cuando trabajo con dos matrices:

 const arr1 = [{ groupId: "Category", options: [{ name: "cookies", id: 1111 }, { name: "tv", id: 2222 }] }, { groupId: "Brand", options: [{ name: "random", id: 3333 }] }, { groupId: "Price", options: [{ name: "random2", id: 4444 }] }], arr2 = [{ id: 1111, url: "/test", other: 'other-prop' }, { id: 2222, url: "/test1" }, { id: 3333, url: "/test2" }], mergeById = (arr1, arr2) => arr1.map(({ options, ...rest }) => ( { ...rest, options: options.map(option => ( { ...option, ...arr2.find(_option => _option.id === option.id) } )) } )); const merged = mergeById(arr1, arr2); console.log(JSON.stringify(merged, null, 2));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const newPosts = posts.map(post => ({ ...post, likes: likes.filter(like => post.likes.map(like => like.id).includes(like.id)), comments: comments.filter(comment => post.comments.map(comment => comment.id).includes(comment.id)), })) console.log(newPosts)
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!