javascript - 即使使用删除运算符,对象属性也不会被删除
问题描述
我有一个对象数组。我想从中删除一个名为 asid
的键。我已使用以下代码将其从对象中删除。
this.state.result.forEach(item => delete item.id);
但是,id 并没有从中删除。我不确定为什么以及我的代码有什么问题。我尝试使用上述逻辑从同一对象中删除其他几个键,并且效果很好。我面临的唯一问题是id
.
我认为这个对象属性是不可配置的。如果是这样,那么我们如何删除它。
有人可以帮我解决这个问题。提前致谢。
解决方案
**
如果您使用的是反应,那么您应该始终使用 setState 来更改状态。
**
解释@Prabu的答案
this.setState({ // calling setState for updating the state
result: this.state.result.map(item => {
let tmpItem = {...item};// storing all the value here in tmpItem
delete tmpItem.id; // deleting the key
return {...tmpItem}; // returning here
})
}, ()=> {
// for immediatly accessing the state you can use callback
});
推荐阅读
- javascript - 上下文菜单 onclick AJAX POST 不起作用
- php - \Eloquent\Model::Create 方法返回 null,状态码 OK
- php - 如何在 VB.net 中转换 JSON 以便在 PHP 中使用
- swift - 仅当 loadRequest 在 viewDidLoad 之外时 UIWebView 的 Nil 错误
- unit-testing - Uber Cadence:如何断言对 workflow.Sleep() 的调用?
- cadence-workflow - Uber Cadence 本地活动与副作用
- laravel - 一些 postgress 连接超时,而另一些则没有
- kubernetes - 我可以使用模板来部署 Kubernetes 吗?
- python-3.x - 如何找到需要优化的代码部分?
- gremlin - 无法添加新顶点