洛达什 在对象内查找对象

lodash find object inside object

本文关键字:对象 查找      更新时间:2023-09-26

如果对象存在,我正在尝试返回 true,如果对象存在

var primary={
  "RHID": {
    "type": "numeric"
  },
  "CD_DOC_ID": {
    "type": "numeric"
  },
  "SEQ": {
    "type": "numeric"
  }
}
console.log(_.contains(primary, 'RHID'))

但客场是假的。谢谢

您可以使用

_.has方法

console.log(_.has(primary, 'RHID'))

RHID是对象primary内部的键,所以你应该在primary的键中查找。

loDash 函数_.keys返回所有对象键的数组,你可以这样使用它:

console.log(_.contains(_.keys(primary), 'RHID')) // true

使用 has() 或 hasIn() 的lodash解决方案:

var primary=
{
    "RHID": {
      "type": "numeric"
    },
    "CD_DOC_ID": {
      "type": "numeric"
    },
    "SEQ": {
      "type": "numeric"
    }
}
console.log(_.has(primary, 'RHID'));

_.has()检查自己的属性,_.hasIn()验证自己的属性和继承的属性。

但最好使用in运算符:

var primary=
{
    "RHID": {
      "type": "numeric"
    },
    "CD_DOC_ID": {
      "type": "numeric"
    },
    "SEQ": {
      "type": "numeric"
    }
}
console.log('RHID' in primary);