Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
Subtract property of an array of object from a preceding object

I have an array of object as such

 "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"
    }
]

I am trying to subtract the PRICE property of each object from that of the preceding object and append the result as profit to that preceding object...

So basically from the first object in the array i'll have;

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

the only thing i've been only to think of is this

            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,
        });

And on the stockData[i - 1].Price i'm getting "can not read property Price of undefined"

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Looking for something like this:

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])

See in action:

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda