如何判断globalSettings对象中是否存在属性值为true?求代码解决方案
Hey there! Let's work through this problem you're having with checking if any property in your globalSettings object is true.
First, let's recap why your previous attempts didn't work:
obj.filter()threw an error becausefilteris an array method, not an object method—you can't call it directly on an object. That makes sense, since you figured that out already!Object.keys(obj).every(k => obj[k] === true)always returnedfalsebecauseevery()only returnstrueif all elements in the array meet the condition. Since onlysetting1istruein your example, this check fails (which is the opposite of what you need).
The Clean, Dynamic Solution
What you actually need is a way to check if at least one property value is true. Here's the concise method you can add to your Vue component:
methods: { checkIfAnyGsIsOverridden(obj) { // Convert object values to an array, then check if any value is strictly true return Object.values(obj).some(value => value === true); } }
How This Works:
Object.values(obj)takes yourglobalSettingsobject ({ setting1: true, setting2: false, setting3: false }) and converts it into an array of its values:[true, false, false].some()checks if any element in the array matches the condition (value === true). As soon as it finds one, it returnstrue—perfect for your use case!
Bonus: Handling Truthy Values (If Needed)
If you ever need to check for any truthy value (not just strict true, like 1, non-empty strings, etc.), you can simplify the condition to:
return Object.values(obj).some(value => value);
But based on your original example, sticking with value === true is the right call to strictly check for boolean true.
Your template code is already correct—once you update the method, it will apply the text-success class whenever any setting in globalSettings is true, and text-muted otherwise.
内容的提问来源于stack exchange,提问作者Syed

