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

257
Vistas
how can remove array of object element without slice, splice

I am trying to delete a single object from the below object of arrays. I have tried with slice but sometimes it doesn't work. That's why I want to try different methods.

{
  id: "6252de4b27082fd83b94e3f4",
  options: [
    { title: "extra Tomato", price: 2 },
    { title: "ketchup", price: 1 },
    { title: "medium", price: 3 },
  ],
  price: 5.1,
  quantity: 1,
  title: "Carroll Montgomery",
}

suppose I want to delete medium object my expected output would be:

{
  id: "6252de4b27082fd83b94e3f4",
  options: [
    { title: "extra Tomato", price: 2 },
    { title: "ketchup", price: 1 },
  ],
  price: 5.1,
  quantity: 1,
  title: "Carroll Montgomery",
}

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can try using the Array.filter method. Just provide the title you want to delete and you would get the deleted array. Doing so, we don't mutate the original array, instead we create a new one with the value removed.

const obj = {
  id: '6252de4b27082fd83b94e3f4',
  options: [
    { title: 'extra Tomato', price: 2 },
    { title: 'ketchup', price: 1 },
    { title: 'medium', price: 3 },
  ],
  price: 5.1,
  quantity: 1,
  title: 'Carroll Montgomery',
}

obj.options = obj.options.filter((item) => item.title !== 'medium')
console.log(obj)

about 4 years ago · Santiago Trujillo Denunciar

0

filter out the item you don't want.

const obj = {
  id: '6252de4b27082fd83b94e3f4',
  options: [
    {title: 'extra Tomato', price: 2},
    {title: 'ketchup', price: 1},
    {title: 'medium', price: 3}
  ],
  price: 5.1,
  quantity: 1,
  title: 'Carroll Montgomery'
};

function removeItem(obj, item) {

  // Destructure the options from the object
  const {options, ...rest } = obj;

  // Return a new object with a filtered
  // array of options
  return {
    ...rest,
    options: options.filter(el => {
      return el.title !== item;
    })
  };

}

console.log(removeItem(obj, 'medium'));

about 4 years ago · Santiago Trujillo Denunciar

0

Here you should just write custom slice function;

var data = {id: "6252de4b27082fd83b94e3f4",
options: [
{title: 'extra Tomato', price: 2},
{title: 'ketchup', price: 1},
{title: 'medium', price: 3}],
price: 5.1,
quantity: 1,
title: "Carroll Montgomery"};

data["options"] = data.options.filter(function(value, index, arr){ 
    return value.title != "medium";
});

console.log(data);

about 4 years ago · Santiago Trujillo 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