javascript - 下一个 ES 中的对象映射
问题描述
我有一个带有状态属性的对象数组:
[{
...
status: 'enable'
},{
...
status: 'enable'
}...]
status 属性可以有 2 个值:'enable'/'disable'
.
如果状态为“启用”,我想将其设置为禁用,反之亦然。
而不是执行以下操作:
if(obj.status === 'enable')
obj.status = 'disable'
else
obj.status = 'enable'
我想将启用状态映射到一个真正的布尔值(并禁用到 false)并执行以下操作:
Mapping[obj.status] = !Mapping[obj.status] // this should change the status value
可能吗?
解决方案
您可以使用一个对象来更改值。
const change = { enable: 'disable', disable: 'enable' };
// in loop
obj.status = change[obj.status];
推荐阅读
- angular - 通过表单组以角度发送复选框值
- flutter - 导航到聊天室后显示聊天室集合
- python - 以并行方式在 3 维数组中查找某个值的索引
- spring - 在存储库中使用 Order By 时 Spring Boot 问题 IllegalArgumentException
- c++ - 如何在不使用 getline() 覆盖的情况下在 char 数组中输入多行
- heroku - 如何设置环境变量 Heroku
- laravel - Laravel 选择旧值和新值作为选择
- java - 从 Ubuntu 18.04 卸载 Oracle Java
- python - 在多行中查找文本以将其替换为 python (xml)
- laravel - 你如何检索这个 Laravel 集合?