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

161
Visualizações
filter array of objects - remove objects when negative and positive values are matching

Array of dates -

const WEXDateArray = WEXFile.map((r) => r.Date);

const uniqueDateArray = WEXDateArray.filter((item, pos) => {
        return WEXDateArray.indexOf(item) == pos;
      });

Date sample -

[
  '8/2/2021',  '8/3/2021',
  '8/4/2021',  '8/5/2021',
  '8/6/2021',  '8/9/2021',
  '8/10/2021', '8/11/2021',
  '8/12/2021', '8/13/2021',
  '8/16/2021', '8/17/2021',
  '8/18/2021', '8/19/2021',
  '8/20/2021', '8/25/2021',
  '8/26/2021', '8/30/2021',
  '8/31/2021'
]

arrays of objects -

const WEXFileArraySeparatedByDates: IWEXInterfaceArrayOfArrays =
        WEXFile.reduce((r, WEX) => {
          r[WEX.Date!] = r[WEX.Date!] || [];
          r[WEX.Date!].push(WEX);
          return r;
        }, Object.create(null));


[
  {
    Date: '8/30/2021',
    'Exec Qty': '15',
    'Call/Put': 'P'
  },
  {
    Date: '8/30/2021',
    'Exec Qty': '13',
    'Call/Put': 'P'
  },
  {
    Date: '8/30/2021',
    'Exec Qty': '8',
    'Call/Put': 'P'
  },
  {
    Date: '8/30/2021',
    'Exec Qty': '14',
    'Call/Put': 'P'
  },
  {
    Date: '8/30/2021',
    'Exec Qty': '12',
    'Call/Put': 'P'
  },
  {
    Date: '8/30/2021',
    'Exec Qty': '-75',
    'Call/Put': 'P'
  {
    Date: '8/30/2021',
    'Exec Qty': '75',
    'Call/Put': 'P'
  }
]
[
  {
    Date: '8/31/2021',
    'Exec Qty': '4000',
    'Call/Put': 'P'
  },
  {
    Date: '8/31/2021',
    'Exec Qty': '2000',
    'Call/Put': 'P'
  }
]

Explanation - I get CSV file from client and I need to delete objects form the array of objects when 2 rows from the same exec qty return 0 when you sum them up. What I did do is to get a array with the dates only. And created a new object with arrays by the dates. What I could not do is the delete objects by this condition -

if there is on Exec Qty [1, -2, 2, 4, 5, 6, -6] return [1, 4, 5]

And after that I'll be able to concat the arrays to new array and return the array without the [-2, 2].

So here is where I'm standing right now -

let finalWEXArray: IWEXInterface[] = [];
      for (const date of uniqueDateArray) {
    finalWEXArray = WEXFileArraySeparatedByDates[date!];
  }

How can I filter the arrays by the conditions I mentioned above and store them in the finalWEXArray

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

0

Not the most efficient solution, but gets the job done:

for (let i = 0; i< arr.length; i++){
  let j = arr.findIndex(x => x.Date === arr[i].Date && +x.qty + +arr[i].qty === 0);

  if (j !== -1) { 
    arr.splice(j, 1)
    arr.splice(i, 1)
    i--;
  }
}

How does it work? It iterates the array, and for each element it checks, if there is an element with the same date but inverse quantity. If such an element is found, remove both. Only increment the index if no element was removed, because otherwise you'd skip some elements in the check.

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