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

160
Vistas
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 Respuestas
Responde la pregunta

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