使用lodash查找null属性

时间:2016-09-14 21:50:19

标签: javascript object properties attributes lodash

我有一个对象,我需要检查atribute是否为null,但总是未定义。

 var obj={
  "EMPRESA": "CMIP",
  "CD_DIRECAO": "01",
  "DT_INI_DIRECAO": "1900-01-01",
  "CD_DEPT": "10",
  "DT_INI_DEPT": "1900-01-01",
  "CD_PROC": "1",
  "DT_INI_PROC": "1900-01-01",
  "DSP_PROC": "Processo 1",
  "DSR_PROC": "Processo 1",
  "DESCRICAO": null,
  "ID_PROCESSO_ARTIGO": null,
  "VALIDACAO_ARTIGO": null,
  "DT_FIM": null,
  "DT_RowId": "row_CMIP011900-01-01101900-01-0111900-01-01",
  "DESIGEMPRESA": "CMIP",
  "DSP_DIRECAO": "Disponivel",
  "DSP_DEPT": "ADM - Administracao"
};
console.log(_.find(obj,{'DT_FIM':null}));

我设置了一个小提琴here

1 个答案:

答案 0 :(得分:1)

对于hole对象包含null attr

_.chain(obj).values().includes(null).value();

对于某些attr

_.isNull(obj['DT_FIM']);

你的varian

_.find(obj,{'DT_FIM':null})

适用于嵌套对象,如

var obj={
    ...
    "DT_FIM": {"DT_FIM":null},
    ...
};

适用于所有null attrs

_.reduce(obj, function(res, val, key) {
    if (_.isNull(val)) {
        res.push(key);
    };    
    return res;
}, [])

对于第一个null attr

_.findKey(obj, _.isNull)