Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!