javascript - Setting all properties of an object to same value
问题描述
Having an object with this structure:
anObject = {
"a_0" : [{"isGood": true, "parameters": [{...}]}],
"a_1" : [{"isGood": false, "parameters": [{...}]}],
"a_2" : [{"isGood": false, "parameters": [{...}]}],
...
};
I want to set all isGood
values to true
. I've tried using _forOwn to go through the object and forEach to go through each property but it seems it's not the correct approach.
_forOwn(this.editAlertsByType, (key, value) => {
value.forEach(element => {
element.isSelected = false;
});
});
The error says:
value.forEach is not a function
解决方案
实际上你非常接近,你需要使用Object.keys()
来获取keys
你的anObject
对象,然后循环它们并最后修改每个array
.
anObject = {
"a_0": [{
"isGood": true,
"parameters": [{}]
}],
"a_1": [{
"isGood": false,
"parameters": [{}],
}],
"a_2": [{
"isGood": false,
"parameters": [{}],
}],
//...
};
Object.keys(anObject).forEach(k => {
anObject[k] = anObject[k].map(item => {
item.isGood = true;
return item;
});
})
console.log(anObject);
推荐阅读
- javascript - Touchstart 和 Touchend 在移动设备上不起作用
- kubernetes - 如何在 kubernetes Dashboard 上将用户限制为一个命名空间?
- python - VS Code Python 脚本第二天突然停止工作
- python - Django app on heroku, ModuleNotFoundError: No module named 'spread'
- android - 如何在 Android Worker 中完成 Kotlin Flow
- git - Mac中的Git碰撞路径
- windows - Unable to install Docker Desktop 3.0.0 onto Windows Server 2019 DataCenter
- java - 访问特定索引处的数组
- sql - 列的验证工作非常缓慢(SQL Server)
- jenkins - Jenkins,Gradle:如何将依赖关系报告发布到 Sonar Dashboard