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

130
Vistas
I want to delete the array whose price is 0

I have an arrayenter image description here I want to delete the price when it is = 0

Why when I want to delete the array whose price is 0 does not delete all array ?

Where is problem ?

var products = [{
    specs: "name",
    shopLink: "linamazon",
    shopLogoUrl: "icon Store/Amazon22.png",
    price: 0,
    priceUpdate: "Amazon.com",
    storeName: "Amazon"
  },
  {
    specs: "name",
    shopLink: "linknewegg",
    shopLogoUrl: "icon Store/NewEgg.png",
    price: 0,
    priceUpdate: "NewEgg.com",
    storeName: "NewEgg"
  },
  {
    specs: "name",
    shopLink: "linknoon",
    shopLogoUrl: "icon Store/noon-logo2.png",
    price: 0,
    priceUpdate: "icon Store/Noon.com",
    storeName: "Noon"
  },
  {
    specs: "name",
    shopLink: "linkebay",
    shopLogoUrl: "icon Store/ebay33.png",
    price: 0,
    priceUpdate: "Ebay.com",
    storeName: "Ebay"
  },
];


for (i = 0; i < products.length; i++) {
  if (products[i].price == 0) {
    products.splice(i, 1)
  }
}

console.log(products)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You're modifying the array in the middle of a loop. This causes the array to shrink, and so i is now referring to a different element. When i increments, you then end up skipping over a value. One possible fix is to change i at the same time you modify the array:

for (i = 0; i < products.length; i++) {
  if (products[i].price == 0 ) {
    products.splice(i, 1)
    i -= 1; // <--------------- added
  }
}

Or you can consider using another approach, such as the .filter method on arrays:

products = products.filter(product => product.price !== 0);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try using something like :

const arr = products.filter(x => x.price > 0);

You can even add casting to Number();

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is what @epascarello and @d-m said in comments, removing them will change the index of items in the array, so your loop will miss a few of them.

And the standard answer is what @aayoub-el-aboussi said

But if you want to keep the for loop for better performance, you just have to reverse the loop, it means try to count from end to start. As the example:

How remove some items in array (without missing) using for loops

var products = [{
    specs: "name",
    shopLink: "linamazon",
    shopLogoUrl: "icon Store/Amazon22.png",
    price: 0,
    priceUpdate: "Amazon.com",
    storeName: "Amazon"
  },
  {
    specs: "name",
    shopLink: "linknewegg",
    shopLogoUrl: "icon Store/NewEgg.png",
    price: 0,
    priceUpdate: "NewEgg.com",
    storeName: "NewEgg"
  },
  {
    specs: "name",
    shopLink: "linknoon",
    shopLogoUrl: "icon Store/noon-logo2.png",
    price: 0,
    priceUpdate: "icon Store/Noon.com",
    storeName: "Noon"
  },
  {
    specs: "name",
    shopLink: "linkebay",
    shopLogoUrl: "icon Store/ebay33.png",
    price: 0,
    priceUpdate: "Ebay.com",
    storeName: "Ebay"
  },
];


for (i = products.length-1; i >=0; i--) {
  if (products[i].price == 0) {
    products.splice(i, 1)
  }
}

console.log(products)

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