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

254
Views
Cómo cambiar valores en un objeto anidado para concatenar con matriz y hacer coincidir con id con javascript

tengo este objeto:

 const obj = { children: [ { value: "1", label: "Organization", Estatus: "1", IdTipo: "1", children: [ { value: "68", label: "REGION 1", IdTipo: "3", Estatus: "1", children: [ { value: "13", label: "Store 1", IdTipo: "3", Estatus: "1", }, { value: "15", label: "Store 2", IdTipo: "3", Estatus: "1", }, { value: "24", label: "Store 3", IdTipo: "3", Estatus: "1", }, ], }, { value: "69", label: "REGION 2", IdTipo: "3", Estatus: "1", children: [ { value: "3", label: "Store 4", IdTipo: "3", Estatus: "1", }, { value: "11", label: "Store 5", IdTipo: "3", Estatus: "1", }, ], }, ], },],};

Y tengo esta matriz

 const arr = [ { "id": 13, "balance": 44958.89 }, { "id": 15, "balance": 55687.89 }, { "id": 24, "balance": 16768.89 }, ]

Necesito hacer coincidir los valores obj y arr id para concatenar la etiqueta y el equilibrio en el objeto. Esperaba esto:

 { children: [ { value: "1", label: "Organization", Estatus: "1", IdTipo: "1", children: [ { value: "68", label: "REGION 1", IdTipo: "3", Estatus: "1", children: [ { value: "13", label: "Store 1 44958.89", IdTipo: "3", Estatus: "1", }, { value: "15", label: "Store 2 55687.89", IdTipo: "3", Estatus: "1", }, { value: "24", label: "Store 3 16768.89", IdTipo: "3", Estatus: "1", }, ], },

Como puede ver, en mi objeto, las etiquetas ahora tienen el nombre y el saldo. No sé si es muy fácil solucionarlo, pero estoy muy confundido y no sé cómo solucionarlo. ¡¡Gracias!!

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

0

Aquí hay una solución con una función recursiva inmutable:

 /** * @typedef {object} OrgElement * @property {string} value * @property {string} label * @property {string} IdTipo * @property {string} Estatus * @property {string} [children] */ /** * * @param {OrgElement[]} orgElements * @param {{id: number, balance: number}[]} balanceData * @returns {OrgElement[]} */ function updateOrgs(orgElements, balanceData) { const data = [...balanceData]; return orgElements.map((orgElement) => { const updatedOrgElement = { ...orgElement }; if (updatedOrgElement.children && data.length > 0) { updatedOrgElement.children = updateOrgs(updatedOrgElement.children, data); } if (updatedOrgElement.value && data.length > 0) { const foundItemIndex = data.findIndex( (item) => item.id.toString() === updatedOrgElement.value ); if (foundItemIndex > -1) { const value = data.splice(foundItemIndex, 1)[0]; updatedOrgElement.label = `${updatedOrgElement.label} ${value.balance}`; } } return updatedOrgElement; }); } const result = { children: updateOrgs(obj.children, arr) };
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!