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

178
Vistas
Splice deleting first items from array of objects instead of index

I'm trying to exclude items from an array of objects based on an array with the indexes I have to remove, but instead it's removing the first items from the array of objects.

My code:

let historicPrecipitation = [{"indicator":"Historic Precipitation","month":"1","year":"2014","value":"228.5"},{"indicator":"Historic Precipitation","month":"2","year":"2014","value":"144.7"},{"indicator":"Historic Precipitation","month":"3","year":"2014","value":"120.3"},{"indicator":"Historic Precipitation","month":"4","year":"2014","value":"146.9"},{"indicator":"Historic Precipitation","month":"5","year":"2014","value":"146.4"},{"indicator":"Historic Precipitation","month":"6","year":"2014","value":"128.1"},{"indicator":"Historic Precipitation","month":"7","year":"2014","value":"139.2"},{"indicator":"Historic Precipitation","month":"8","year":"2014","value":"150.0"},{"indicator":"Historic Precipitation","month":"9","year":"2014","value":"199.7"},{"indicator":"Historic Precipitation","month":"10","year":"2014","value":"268.4"},{"indicator":"Historic Precipitation","month":"11","year":"2014","value":"98.5"},{"indicator":"Historic Precipitation","month":"12","year":"2014","value":"139.9"}];

let monthsWithoutRegistration = [4, 5];

for (let i = 0; i < monthsWithoutRegistration.length; i++) {
    for (let j = 0; j < historicPrecipitation.length; j++) {
        if (parseInt(historicPrecipitation[j]['month']) === parseInt(monthsWithoutRegistration[i])) {
            historicPrecipitation.splice(historicPrecipitation[j], 1);
        }
    }
}

console.log(historicPrecipitation);

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

0

You need use j instead of historicPrecipitation[j] in splice because you should pass index not object:

let historicPrecipitation = [{
  "indicator": "Historic Precipitation",
  "month": "1",
  "year": "2014",
  "value": "228.5"
}, {
  "indicator": "Historic Precipitation",
  "month": "2",
  "year": "2014",
  "value": "144.7"
}, {
  "indicator": "Historic Precipitation",
  "month": "3",
  "year": "2014",
  "value": "120.3"
}, {
  "indicator": "Historic Precipitation",
  "month": "4",
  "year": "2014",
  "value": "146.9"
}, {
  "indicator": "Historic Precipitation",
  "month": "5",
  "year": "2014",
  "value": "146.4"
}, {
  "indicator": "Historic Precipitation",
  "month": "6",
  "year": "2014",
  "value": "128.1"
}, {
  "indicator": "Historic Precipitation",
  "month": "7",
  "year": "2014",
  "value": "139.2"
}, {
  "indicator": "Historic Precipitation",
  "month": "8",
  "year": "2014",
  "value": "150.0"
}, {
  "indicator": "Historic Precipitation",
  "month": "9",
  "year": "2014",
  "value": "199.7"
}, {
  "indicator": "Historic Precipitation",
  "month": "10",
  "year": "2014",
  "value": "268.4"
}, {
  "indicator": "Historic Precipitation",
  "month": "11",
  "year": "2014",
  "value": "98.5"
}, {
  "indicator": "Historic Precipitation",
  "month": "12",
  "year": "2014",
  "value": "139.9"
}];

let monthsWithoutRegistration = [4, 5];

for (let i = 0; i < monthsWithoutRegistration.length; i++) {
  for (let j = 0; j < historicPrecipitation.length; j++) {
    if (parseInt(historicPrecipitation[j]['month']) === parseInt(monthsWithoutRegistration[i])) {
      historicPrecipitation.splice(j, 1);
    }
  }
}

console.log(historicPrecipitation);

about 4 years ago · Juan Pablo Isaza Denunciar

0

The splice syntax is wrong,

replace with this

  historicPrecipitation.splice(j, 1);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can make use of filter instead of the nested for loops:

let filteredHistoricPrecipitation = historicPrecipitation.filter(x => 
    !monthsWithoutRegistration.includes(parseInt(x['month']))
);
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