You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于数组内的键修改对象对应布尔值的实现方案

Solution: Modify Object Boolean Values for Keys in a Target Array

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 to indexOf() !== -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 with false; 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:23:10