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

103
Views
Fusionar objeto donde la clave coincide

Soy nuevo en mecanografiar o node.js, estoy tratando de combinar la siguiente matriz JSON Nota: No estoy seguro de si el cliente permite nuevas dependencias o complementos.

 [ { parentauthscopekey: 1, childauthscopekey: 2 }, { parentauthscopekey: 12, childauthscopekey: 10 }, { parentauthscopekey: 12, childauthscopekey: 11 }, { parentauthscopekey: 13, childauthscopekey: 1 } ]

A

 [ { parentauthscopekey: 1, childauthscopekey: [ 2, 1 ] }, { parentauthscopekey: 12, childauthscopekey: [ 10, 11, 12 ] }, { parentauthscopekey: 13, childauthscopekey: [ 1, 13 ] } ]

Intenté el siguiente código, son demasiadas líneas pero hace el trabajo, pero esperaba algo simple sin usar el alcance codificado y los valores de scopeMap

 table = [ { parentauthscopekey: 1, childauthscopekey: 2 }, { parentauthscopekey: 12, childauthscopekey: 10 }, { parentauthscopekey: 12, childauthscopekey: 11 }, { parentauthscopekey: 13, childauthscopekey: 1 } ] scope=[1,12,13] scopeMap = new Set() table2 = [] scope.forEach(parentauthscopekey => { table.forEach(row =>{ if(row.parentauthscopekey == parentauthscopekey && !scopeMap.has(parentauthscopekey)) { scopeMap.add(parentauthscopekey) childauthscopekey = [] table.filter(c => c.parentauthscopekey == row.parentauthscopekey).forEach(r=>{ childauthscopekey.push(r.childauthscopekey) }) childauthscopekey.push(parentauthscopekey) table2.push({parentauthscopekey, childauthscopekey}) } }) }) console.log(table2)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

puedes :

  • use array.reduce para construir la estructura esperada
  • recuperar o cada entrada del elemento secundario que coincida con parentauthscopekey con array.filter
  • y agregando la clave parentauthscope actual a childauthscopekey

 var data = [ { parentauthscopekey: 1, childauthscopekey: 2 }, { parentauthscopekey: 12, childauthscopekey: 10 }, { parentauthscopekey: 12, childauthscopekey: 11 }, { parentauthscopekey: 13, childauthscopekey: 1 } ]; var result = data.reduce((acc, current) => { if (!acc.some(one => one.parentauthscopekey === current.parentauthscopekey)) { var childs = data.filter(one => one.parentauthscopekey === current.parentauthscopekey).map(one => one.childauthscopekey); childs.push(current.parentauthscopekey); acc.push({ parentauthscopekey: current.parentauthscopekey, childauthscopekey: childs }) } return acc; }, []); console.log(result);

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!