admin管理员组

文章数量:1335847

I have an object, object values are boolean (true or false). If object has only one true value then return true otherwise return false.

I tried this:

var obj = {1001: false, 1002: true};

var keys = Object.keys(obj);

var filtered = keys.filter(function(key) {
    return obj[key] === true
});

console.log(filtered)

I have an object, object values are boolean (true or false). If object has only one true value then return true otherwise return false.

I tried this:

var obj = {1001: false, 1002: true};

var keys = Object.keys(obj);

var filtered = keys.filter(function(key) {
    return obj[key] === true
});

console.log(filtered)
Share Improve this question edited Apr 15, 2020 at 12:58 j3ff 6,1198 gold badges43 silver badges54 bronze badges asked Apr 15, 2020 at 11:39 GOOGGOOG 731 silver badge9 bronze badges 4
  • more than TRUE Eg, prop: 'foo', prop2: true should result in false, is that what you mean? – CertainPerformance Commented Apr 15, 2020 at 11:41
  • {1001: true, 1002: true} ==> FALSE – GOOG Commented Apr 15, 2020 at 11:42
  • {1001: false, 1002: true,1003: false} ==> TRUE – GOOG Commented Apr 15, 2020 at 11:43
  • {1001: false, 1002: false,1003: false} ===> FALSE – GOOG Commented Apr 15, 2020 at 11:44
Add a ment  | 

6 Answers 6

Reset to default 4

try this

var filtered = Object.values(test).filter(item => item).length === 1;
console.log(filtered)

You'll have to iterate over all values regardless, in order to see if they're all true.

If the values are guaranteed to be booleans, one option would be to add them all up. If the result is equal to the number of properties, or 0, return false, otherwise return true:

const checkValid = obj => {
  const values = Object.values(obj);
  const truthyValues = values.reduce((a, b) => a + b, 0);
  return truthyValues > 0 && truthyValues !== values.length;
};
console.log(
  checkValid({1001: true, 1002: true}),
  checkValid({1001: false, 1002: true,1003: false}),
  checkValid({1001: false, 1002: false,1003: false}),
);

You could count with a short ciruit for more than one true value.

const one = object => {
    let count = -1;
    return !Object.values(object).some(v => v === true && ++count) && !count;
};

console.log(one({ 1001: false, 1002: true }));               //  true
console.log(one({ 1001: true, 1002: true }));                // false
console.log(one({ 1001: false, 1002: true, 1003: false }));  //  true
console.log(one({ 1001: false, 1002: false, 1003: false })); // false

Plain and simple Working code is attached, to check result just increase or decrease the number of true in Object

    const ob = {1001: false, 1002: false, 1003: true, 1005: true};

var hasTrue = Object.keys(ob).some(k => ob[k]);
var bb = Object.values(ob).reduce((a, item) => a + item, 0);
if(bb > 1){
    alert("False");
}else {
    alert("true");

const ob = {1001: false, 1002: false, 1003: true, 1005: true};

var hasTrue = Object.keys(ob).some(k => ob[k]);
var bb = Object.values(ob).reduce((a, item) => a + item, 0);
if(bb > 1){
  alert("False");
}else {
alert("true");
}

}

The easiest way to check the object is some() function built in Javascript which is built for exactly this purposes.

Definition: The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

Usage in your case:

const checkValid = obj => {
  var obj = {1001: false, 1002: true};
  return Object.values(obj).some(element => element === true)
};

First, we use Object.values(obj) to get the values of the object as an array. Then, we go through the array and check if it has at least one true value.

More on some() function: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

If I understand correct your request, then please try to use:

const obj = {1001: true, 1002: false};
const values = Object.values(obj);
const result = values.includes(true) && values.every((val) => val === true) ? true : false;

or as another one:

const obj = {1001: true, 1002: false};
const result = Object.values(obj).some(val => val === true);

本文标签: object has at least one TRUE then return true in javascriptStack Overflow