[英]iterate over object and change object value
I have the following object: 我有以下对象:
orderSummeryPopin: {
billingAddress: false,
shippingAddress: false,
changeAddress: false
}
Now I would like to iterate over the object and if the key is equal to the parameter I pass to the function I would like to change the value of that particular key to "true" while setting the others to false . 现在,我想遍历该对象,如果键等于传递给函数的参数,我想将该特定键的值更改为“ true”, 同时将其他键设置为false 。
My function would look kind of like this: 我的函数看起来像这样:
showPopin(selectedPopin) {
for (var popin in this.orderSummeryPopin) {
if (selectedPopin === popin) {
popin = true;
}
}
}
I know that 我知道
popin = true
doesn't change anything on the object itself. 不会更改对象本身的任何内容。 So how can I achieve that? 那么我该如何实现呢?
This should do the trick. 这应该可以解决问题。 It'll iterate over each key/value pair in the original object, and set the value to true if the current key
name matches selectedPopin
and to false otherwise. 它将遍历原始对象中的每个键/值对,如果当前key
名与selectedPopin
匹配,则将该值设置为true,否则将其设置为false。
function showPopin(selectedPopin) {
for(var key in orderSummeryPopin) {
orderSummeryPopin[key] = selectedPopin === key;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.