lodash check object properties has values

别说谁变了你拦得住时间么 提交于 2019-12-06 19:50:41

问题


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 check whether that object's property is not empty or has value or not? If array there's a isEmpty method.

I tried this

const somethingKeyIsnotEmpty = Object.keys((props.something, key, val) => {
        return val[key] !== '' || val[key] !== undefined
})

回答1:


In lodash, you can use _.some

_.some(props.something, _.isEmpty)



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/44962321/lodash-check-object-properties-has-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!