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

308
Visualizações
javascript - combine the object if the value of its field is the same

Below is the current data

const data = [
  { id: 1, type: "call", strike: 3000, volume: 50000 },
  { id: 2, type: "call", strike: 5000, volume: 30000 },
  { id: 3, type: "call", strike: 7000, volume: 20000 },
  { id: 4, type: "put", strike: 3000, volume: 50000 },
  { id: 5, type: "put", strike: 5000, volume: 10000 },
  { id: 6, type: "put", strike: 7000, volume: 7000 }
]

I want to do some processing of the data based on strike, which means the object with same value in strike will be combined, so that it becomes something like

[
  { strike: 3000, callVolume: 50000, putVolume: 50000 },
  { strike: 5000, callVolume: 30000, putVolume: 10000 },
  { strike: 7000, callVolume: 20000, putVolume: 7000 },
]

How should I do it in javascript?

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

0

This is really just a 'group by' by strike with a computed property name based on type.

const data = [    { id: 1, type: 'call', strike: 3000, volume: 50000 },    { id: 2, type: 'call', strike: 5000, volume: 30000 },    { id: 3, type: 'call', strike: 7000, volume: 20000 },    { id: 4, type: 'put', strike: 3000, volume: 50000 },    { id: 5, type: 'put', strike: 5000, volume: 10000 },    { id: 6, type: 'put', strike: 7000, volume: 7000 },  ];

const result = Object.values(
  data.reduce((a, { type, strike, volume }) => {
    (a[strike] ??= { strike })[`${type}Volume`] = volume;

    return a;
  }, {})
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could collect volume by each group.

const
    data = [{ id: 1, type: "call", strike: 3000, volume: 50000 }, { id: 2, type: "call", strike: 5000, volume: 30000 }, { id: 3, type: "call", strike: 7000, volume: 20000 }, { id: 4, type: "put", strike: 3000, volume: 50000 }, { id: 5, type: "put", strike: 5000, volume: 10000 }, { id: 6, type: "put", strike: 7000, volume: 7000 }],
    result = Object.values(data.reduce((r, { type, strike, volume }) => {
        r[strike] ??= { strike, callVolume: 0, putVolume: 0 };
        r[strike][type + 'Volume'] += volume;
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a forEach/for loop to init a item in result object. By using Array.find use can check the result exists or not to init a new result item.

const data = [
    { id: 1, type: "call", strike: 3000, volume: 50000 },
    { id: 2, type: "call", strike: 5000, volume: 30000 },
    { id: 3, type: "call", strike: 7000, volume: 20000 },
    { id: 4, type: "put", strike: 3000, volume: 50000 },
    { id: 5, type: "put", strike: 5000, volume: 10000 },
    { id: 6, type: "put", strike: 7000, volume: 7000 }
]

let result = [];
data.forEach(item => {
  let result_item = result.find(e => e.strike == item.strike);
  var isNew = false;
  if (!result_item) {
    result_item = {
      strike: item.strike
    };
    isNew = true;
  }

  if (item.type == "call")
    result_item.callVolume = item.volume;
  else
    result_item.putVolume = item.volume;

  if (isNew) result.push(result_item);
});

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