最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to remove a key from JSON Array in ES6 - Stack Overflow

programmeradmin1浏览0评论

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);

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 (mented lines), it is showing me undefined.

Where am I going wrong?

Share Improve this question edited Sep 14, 2021 at 8:22 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Aug 23, 2021 at 17:18 RakeshRakesh 5941 gold badge10 silver badges27 bronze badges 4
  • 3 forEach doesn't return a value so you will get undefined – AlexSp3 Commented Aug 23, 2021 at 17:19
  • thanks for this @AlexandroPalacios, so I think I can achieve my requirement using map rather than forEach. Is there any better way that you remend ? – Rakesh Commented Aug 23, 2021 at 17:20
  • 1 JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. – T.J. Crowder Commented Aug 23, 2021 at 17:21
  • 1 @Rakesh yes you can using map. There is an answer below ;) – AlexSp3 Commented Aug 23, 2021 at 17:22
Add a ment  | 

5 Answers 5

Reset to default 4

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);

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);

forEach doesn't return a value. Any value returned in the callback given to forEach is going nowhere.

Don't do this with .map either, because delete is mutating the objects. Your approach with a for loop is the right one.

It would be different if you would want to avoid object mutation, and would like to create entirely new objects and put those in a new array. In that case .map() is the way to go:

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

let result = arr.map(({YYY, ...rest}) => rest);

console.log(result);

Here we isolate the YYY property with destructuring notation, collecting the other properties in rest, which is then the object you really want to have.

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);

use obj.map instead of obj.forEach

发布评论

评论列表(0)

  1. 暂无评论