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

119
Visualizações
Restar la propiedad de una matriz de objetos de un objeto anterior

Tengo una matriz de objetos como tal.

 "stock": [ { "Quantity": -36, "Price": 74.55, "Consideration": 2683.8, "Direction": "SELL" }, { "Quantity": -50, "Price": 74.14, "Consideration": 3707, "Direction": "SELL" }, { "Quantity": 12, "Price": 77.15, "Consideration": -925.8, "Direction": "BUY" }, { "Quantity": 15, "Price": 76.4, "Consideration": -1146, "Direction": "BUY" }, { "Quantity": 19, "Price": 75.8, "Consideration": -1440.2, "Direction": "BUY" }, { "Quantity": 20, "Price": 82.44, "Consideration": -1648.8, "Direction": "BUY" }, { "Quantity": 10, "Price": 82.08, "Consideration": -820.8, "Direction": "BUY" }, { "Quantity": 10, "Price": 82.49, "Consideration": -824.9, "Direction": "BUY" } ]

Estoy tratando de restar la propiedad PRECIO de cada objeto de la del objeto anterior y agregar el resultado como ganancia a ese objeto anterior...

Entonces, básicamente, desde el primer objeto en la matriz que tendré;

 74.55 - 74.14 and the result will be appended to the second object as profit: "result:

lo único en lo que he estado pensando es en esto

 let stockData = stock.map((stock) => { return { Quantity: stock.Quantity, Price: stock.Price, Consideration: stock.Consideration, Direction: stock.Direction, }; }); for (let i = 0; i < stockData.length; i++) { let profit = stockData[i].Price - stockData[i - 1].Price; } res.status(200).json({ stock: stockData, });

Y en stockData[i - 1].Precio obtengo "no se puede leer la propiedad Precio de indefinido"

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Buscando algo como esto:

 const result = stock.map((a,b) => (a.profit = a.Price - stock[b+1]?.Price || 0, a)); // map over stock array and set profit property of first element to difference between this and the next element (stock[b+1])

Ver en acción:

 const stock = [{ "Quantity": -36, "Price": 74.55, "Consideration": 2683.8, "Direction": "SELL" }, { "Quantity": -50, "Price": 74.14, "Consideration": 3707, "Direction": "SELL" }, { "Quantity": 12, "Price": 77.15, "Consideration": -925.8, "Direction": "BUY" }, { "Quantity": 15, "Price": 76.4, "Consideration": -1146, "Direction": "BUY" }, { "Quantity": 19, "Price": 75.8, "Consideration": -1440.2, "Direction": "BUY" }, { "Quantity": 20, "Price": 82.44, "Consideration": -1648.8, "Direction": "BUY" }, { "Quantity": 10, "Price": 82.08, "Consideration": -820.8, "Direction": "BUY" }, { "Quantity": 10, "Price": 82.49, "Consideration": -824.9, "Direction": "BUY" } ] const result = stock.map((a,b) => (a.profit = a.Price - stock[b+1]?.Price || 0, a)); console.log(result)

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