admin管理员组文章数量:1221386
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
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:
Share Improve this question asked May 9, 2018 at 13:17 Leo MessiLeo Messi 6,17622 gold badges77 silver badges153 bronze badges 1 |value.forEach is not a function
3 Answers
Reset to default 8actually you were very close, you need to use Object.keys()
to get the keys
of your anObject
object and then loop over them and finally modify each 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);
Use forEach()
and map()
on object anObject
var anObject = {
"a_0" : [{"isGood": true, "parameters": []}],
"a_1" : [{"isGood": false, "parameters": []}],
"a_2" : [{"isGood": false, "parameters": []}]
};
Object.keys(anObject).forEach((key)=>{
anObject[key].map(obj => obj.isGood = true);
});
console.log(anObject);
Try this simple:
for (var key in anObject) {
anObject[key]["isGood"] = true;
}
本文标签: javascriptSetting all properties of an object to same valueStack Overflow
版权声明:本文标题:javascript - Setting all properties of an object to same value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739266463a2155620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
value
? – nilsK Commented May 9, 2018 at 13:21