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

218
Visualizações
Unable to remove a key from JSON Array in ES6

I'm writing a JS code in which I want to delete a specific key from JSON Array and my code is as below.

var obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

for (var i = 0; i < obj.length; i++) {
    delete obj[i].YYY
}


/*obj=obj.forEach(item =>{ return delete item.YYYY});*/

console.log(obj);

This code works fine and is giving me the result as expected, but, When I try to run it using forEach (commented lines), it is showing me undefined.

Where am I going wrong?

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

0

Just remove the obj = part of your code. forEach always returns undefined. Since you're modifying the objects in your array and you want to replicate the for loop, forEach is fine, just don't reassign (and no need for return in the callback):

obj.forEach(item => { delete item.YYY; });

Live Example:

var obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

obj.forEach(item => { delete item.YYY; });

console.log(obj);

(I removed the typo from the forEach code, you had an extra Y.)

That said, there's nothing wrong with a for loop, or you could use a for-of loop:

for (const item of obj
    delete item.YYY;
}

Live Example:

var obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

for (const item of obj
    delete item.YYY;
}

console.log(obj);

Another alternative is to create a new array with new objects that don't have the YYY, via map and perhaps with destructuring:

obj = obj.map(({YYY, ...rest}) => rest);

Live Example:

let obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

obj = obj.map(({YYY, ...rest}) => rest);

console.log(obj);

about 4 years ago · Juan Pablo Isaza Relatório

0

forEach doesn't return a value.

var obj = [{
    XXX: "2",
    YYY: "3",
    ZZZ: "4"
  },
  {
    XXX: "5",
    YYY: "6",
    ZZZ: "7"
  },
  {
    XXX: "1",
    YYY: "2",
    ZZZ: "3"
  }
];



obj.forEach(item => delete item.YYY);

console.log(obj);

If you don't want to mutate the original array, you can use Array.map:

var obj = [{
    XXX: "2",
    YYY: "3",
    ZZZ: "4"
  },
  {
    XXX: "5",
    YYY: "6",
    ZZZ: "7"
  },
  {
    XXX: "1",
    YYY: "2",
    ZZZ: "3"
  }
];



obj = obj.map(item => (delete item.YYY, item));

console.log(obj);

about 4 years ago · Juan Pablo Isaza Relatório

0

The Return value value of Array.prototype.forEach() is undefined, the actual object is modified. Also, you can shorter your code by removing the curly braces ({...}) and return keyword:

var obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];


obj.forEach(item => delete item.YYY);
console.log(obj);

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