P粉1116277872023-08-29 00:57:11
You can try using Array.prototype.reduce
and Object.entries
like this to loop through the key/value pairs of sections
and set them as needed true
/false
, thus creating a new sections
object:
const updateSections = (section) => { sections = Object.entries(sections).reduce((acc, [key, value]) => { // 如果键与选定的section匹配,设置为true if (key === section) return { ...acc, [key]: true }; // 否则设置为false return { ...acc, [key]: false }; }, {}); } // 以你想要的方式触发updateSections来更新特定的section v-on:click="updateSections('section1')"
If you find that you need to modify the properties directly, you can use Array.prototype.forEach
:
const updateSections = (section) => { Object.keys(sections).forEach(key => { // 如果键与选定的section匹配,设置为true if (key === section) sections[key] = true; // 否则设置为false sections[key] = false; }); }
Hope it helps!