object has at least one TRUE then return true in javascript - Stack Overflow

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

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742399105a4436547.html

相关推荐

  • object has at least one TRUE then return true in javascript - Stack Overflow

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

    15小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信