最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodash find object inside object - Stack Overflow

programmeradmin2浏览0评论

i am trying to return true if object exists with

var primary={
  "RHID": {
    "type": "numeric"
  },
  "CD_DOC_ID": {
    "type": "numeric"
  },
  "SEQ": {
    "type": "numeric"
  }
}

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

But aways get false. Thanks

i am trying to return true if object exists with

var primary={
  "RHID": {
    "type": "numeric"
  },
  "CD_DOC_ID": {
    "type": "numeric"
  },
  "SEQ": {
    "type": "numeric"
  }
}

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

But aways get false. Thanks

Share Improve this question edited Jan 27, 2016 at 13:04 suvroc 3,0621 gold badge17 silver badges29 bronze badges asked Jan 27, 2016 at 12:27 Leonel Matias DomingosLeonel Matias Domingos 2,0806 gold badges36 silver badges54 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can use _.has method

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

RHID is a key inside the object primary, so you should look up in the keys of primary.

loDash function _.keys returns an array of all the object keys, yo ucan use it this way:

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

A lodash solution using has() or hasIn():

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

_.has() checks for own properties, _.hasIn() verifies for own and inherited ones.

But it would be better to use in operator:

var primary=
{
    "RHID": {
      "type": "numeric"
    },
    "CD_DOC_ID": {
      "type": "numeric"
    },
    "SEQ": {
      "type": "numeric"
    }
}

console.log('RHID' in primary);
发布评论

评论列表(0)

  1. 暂无评论