Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
necesita cambiar la estructura obj

Tengo el siguiente resultado de la base de datos:

 [ { "id": 6, "lineItem": "PC COST", "lineItemAmount": 1000, "fiscalYear": 2022, "routing_Tool_Id": 1 }, { "id": 10, "lineItem": "PC COST", "lineItemAmount": 100, "fiscalYear": 2023, "routing_Tool_Id": 1 }, { "id": 11, "lineItem": "Travel", "lineItemAmount": 10, "fiscalYear": 2024, "routing_Tool_Id": 1 }, { "id": 12, "lineItem": "Travel", "lineItemAmount": 100, "fiscalYear": 2026, "routing_Tool_Id": 1 } ]

Sin embargo, debe convertirse de la siguiente forma: cada FY tiene el valor de lineAmount

 [ { "LineItem": "PC COST", "currentFy": 1000, "currentFy01": 100, "currentFy02": null, "currentFy03": null, "currentFy04": null, "currentFy05": null }, { "LineItem": "Travel", "currentFy": null, "currentFy01": null, "currentFy02": null, "currentFy03": 10, "currentFy04": null, "currentFy05": 100 } ]

esto es lo que tengo hasta ahora: he pasado días buscando una forma limpia de hacerlo.

 const handleMatrix = (obj) => { let rowData = []; let index = []; // iterate obj and return unique line items function groupBy(objectArray, property) { return objectArray.reduce(function (acc, obj) { let key = obj[property] if (!acc[key]) { acc[key] = [] } acc[key].push(obj) return acc }, {}) } // assign unique line items to be used as main index for new obj index = groupBy(obj, 'lineItem'); //console.log(index); Object.keys(index).map((lineItem) => ( rowData.push({ "LineItem": lineItem, "currentFy": null, "currentFy01": null, "currentFy02": null, "currentFy03": null, "currentFy04": null, "currentFy05": null, }) ) ) // will update with current FY - function will be here updating array rowData[0]["currentFy"] = 2000 return rowData }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Para cada artículo:

  • Si aún no existe un objeto para LineItem, créelo
  • Calcule la diferencia del año fiscal y, por lo tanto, lineItemAmount se puede asignar a la ubicación adecuada

Finalmente, convierta el objeto de objetos en una matriz de objetos.

 const input=[{id:6,lineItem:"PC COST",lineItemAmount:1e3,fiscalYear:2022,routing_Tool_Id:1},{id:10,lineItem:"PC COST",lineItemAmount:100,fiscalYear:2023,routing_Tool_Id:1},{id:11,lineItem:"Travel",lineItemAmount:10,fiscalYear:2024,routing_Tool_Id:1},{id:12,lineItem:"Travel",lineItemAmount:100,fiscalYear:2026,routing_Tool_Id:1}]; const initialFy = 2022; const amountsByType = {}; const initialObj = { "currentFy": null, "currentFy01": null, "currentFy02": null, "currentFy03": null, "currentFy04": null, "currentFy05": null, }; for (const { lineItem, lineItemAmount, fiscalYear } of input) { amountsByType[lineItem] ??= { LineItem: lineItem, ...initialObj }; const fyDiff = fiscalYear - initialFy; if (fyDiff > 5 || fyDiff < 0) continue; const prop = 'currentFy' + (fyDiff === 0 ? '' : '0' + fyDiff); amountsByType[lineItem][prop] = lineItemAmount; } console.log(Object.values(amountsByType));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda