use*_*206 2 javascript arrays object ecmascript-6
我正在学习 ES6 和对象,想知道如何删除这个对象数组中的一个键值对:
[
{ "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
{ "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
{ "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
]
Run Code Online (Sandbox Code Playgroud)
结果应该是:
[
{ "name" : "mark", "height" : "tall", "nat" : "uk"},
{ "name" : "ben", "height" : "medium", "nat" : "uk"},
{ "name" : "neil", "height" : "small", "nat" : "uk" }
]
Run Code Online (Sandbox Code Playgroud)
我创建了一个 forEach 函数并尝试将每个结果推送到一个新数组中,但现在没有对象。
如何解决这个问题?或者有更好的方法使用 ES6/ES7 语法来做到这一点吗?谢谢你的帮助。代码和codePen URL如下:
代码笔: https: //codepen.io/anon/pen/bPYQyb
[
{ "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
{ "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
{ "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
]
Run Code Online (Sandbox Code Playgroud)
objArr.forEach(a => delete a.theId);
Run Code Online (Sandbox Code Playgroud)
删除运算符的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete