[英]Remove property for object in array
I have next code, where I need remove property unit
from object Item
in array Items
:我有下一个代码,我需要从数组
Items
中的 object Item
中删除属性unit
:
Items.map(({ Item.unit, ...rest }) => {
// some code
return rest;
});
But I am getting next error: Unexpected token., expected
但我收到下一个错误:
Unexpected token., expected
You are not destructuring correctly.您没有正确解构。
As you map over the Items
array, each object in Items
array will be passed as first argument to the callback function of map
function. As you map over the
Items
array, each object in Items
array will be passed as first argument to the callback function of map
function. You can either just use any parameter name in the callback function or destructure the object inside function's parameter list您可以在回调 function 中使用任何参数名称,也可以在函数的参数列表中解构 object
If you use any parameter name as first argument to callback function, then you can remove the unit
property as:如果您使用任何参数名称作为回调 function 的第一个参数,则可以将
unit
属性删除为:
Items.map(item => {
// some code
const {unit, ...rest} = item;
return rest;
});
but if you want to destructure the object in function's parameter list, then you need to do this as但是如果你想在函数的参数列表中解构 object,那么你需要这样做
Items.map(({ unit, ...rest }) => {
// some code
return rest;
});
what if I need 'Item' to access some other property?
如果我需要“项目”来访问其他属性怎么办?
all the properties of the currently destructured object, except unit
property, will be available on rest
object当前解构的 object 的所有属性,除了
unit
属性,将在rest
object 上可用
You can delete
it:你可以
delete
它:
Items.map(item => {
delete item.unit;
return item;
});
Items.map(({unit, ...rest}) => {return rest});
You don't need to explicitly write return or event spread it while returning.您无需在返回时显式编写返回或事件传播它。
return Items.map(({unit, ...withoutUnit}) => withoutUnit)
You'll get a new array after this.
之后你会得到一个新数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.