How to know if all javascript object values are true?

前端 未结 5 874
再見小時候
再見小時候 2020-12-23 16:21

In JavaScript, I need to know if all object items are set to true.

If I have the following object:

var myObj = {title:true, name:true, email:false};
         


        
5条回答
  •  有刺的猬
    2020-12-23 16:51

    You can use every from lodash

    const obj1 = { a: 1, b: 2, c: true };
    const obj2 = { a: true, b: true, c: true }; 
    
    _.every(obj1, true);  // false
    _.every(obj2, true);  // true
    

提交回复
热议问题