lodash check object properties has values

后端 未结 3 1364
北海茫月
北海茫月 2021-01-11 09:46

I have object with several properties, says it\'s something like this

{ a: \"\", b: undefined }

in jsx is there any one line solution I can

相关标签:
3条回答
  • 2021-01-11 10:04

    In lodash, you can use _.some

    _.some(props.something, _.isEmpty)
    
    0 讨论(0)
  • 2021-01-11 10:22

    You can use lodash _.every and check if _.values are _.isEmpty

    const profile = {
      name: 'John',
      age: ''
    };
    
    const emptyProfile = _.values(profile).every(_.isEmpty);
    
    console.log(emptyProfile); // returns false
    
    0 讨论(0)
  • 2021-01-11 10:29

    Possible ways:

    Iterate all the keys and check the value:

    let obj = {a:0, b:2, c: undefined};
    
    let isEmpty = false;
    
    Object.keys(obj).forEach(key => {
        if(obj[key] == undefined)
            isEmpty = true;
    })
    
    console.log('isEmpty: ', isEmpty);

    Use Array.prototype.some(), like this:

    let obj = {a:0, b:1, c: undefined};
    
    let isEmpty = Object.values(obj).some(el => el == undefined);
    
    console.log('isEmpty: ', isEmpty);

    Check the index of undefined and null:

    let obj = {a:1, b:2, c: undefined};
    
    let isEmpty = Object.values(obj).indexOf(undefined) >= 0;
    
    console.log('isEmpty: ', isEmpty);

    0 讨论(0)
提交回复
热议问题