如何基于数组内的键修改对象对应布尔值的实现方案
Got it, let's break down how to solve this problem. You need to update an object's boolean values—specifically setting them to false for any keys that appear in a given array. Here are a few clean, vanilla JavaScript approaches to do this:
Method 1: Simple Loop with Object.keys() and includes()
This is straightforward and works great for most common use cases:
// Your original object const statusObj = { '9': true, '10': false, '11': true, '12': true, '13': true, '14': true, '15': true, '16': true, '17': true, '18': true, '19': true }; // Keys that need their values set to false const keysToFlip = ['10','15','18']; // Iterate over each key in the object Object.keys(statusObj).forEach(key => { // Check if the key is in our target array if (keysToFlip.includes(key)) { statusObj[key] = false; } }); console.log(statusObj); // Output matches your desired result: // { '9': true, '10': false, '11': true, '12': true, '13': true, '14': true, '15': false, '16': true, '17': true, '18': false, '19': true }
How it works:
Object.keys(statusObj)extracts all the object's keys into an array.- We loop through each key with
forEach(). keysToFlip.includes(key)checks if the current key is in our target array (a more readable alternative toindexOf() !== -1).- If it's a match, we set the key's value to
false.
Method 2: Optimized Lookup with a Set
If you're working with a large object or target array, using a Set will speed up the membership check (since Set.has() is O(1) vs. Array.includes() which is O(n)):
const statusObj = { '9': true, '10': false, '11': true, '12': true, '13': true, '14': true, '15': true, '16': true, '17': true, '18': true, '19': true }; const keysToFlip = ['10','15','18']; // Convert the target array to a Set for faster lookups const keySet = new Set(keysToFlip); Object.keys(statusObj).forEach(key => { if (keySet.has(key)) { statusObj[key] = false; } });
Method 3: Immutably Create a New Object
If you don't want to modify the original object (e.g., for state management in frameworks like React), use Object.fromEntries() to create a new object instead:
const statusObj = { '9': true, '10': false, '11': true, '12': true, '13': true, '14': true, '15': true, '16': true, '17': true, '18': true, '19': true }; const keysToFlip = ['10','15','18']; const keySet = new Set(keysToFlip); // Create a new object without mutating the original const updatedStatusObj = Object.fromEntries( Object.entries(statusObj).map(([key, value]) => [ key, keySet.has(key) ? false : value ]) ); console.log(updatedStatusObj); // Desired result console.log(statusObj); // Original object remains unchanged
How it works:
Object.entries(statusObj)converts the object into an array of[key, value]pairs.- We
map()over each pair: if the key is in the Set, we replace the value withfalse; otherwise, we keep the original value. Object.fromEntries()converts the modified array back into a new object.
All these approaches will get you to your desired output—pick the one that best fits your use case!
内容的提问来源于stack exchange,提问作者Mel Pacheco

