Leonel Matias Domingos
Leonel Matias Domingos

Reputation: 2040

lodash find object inside object

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

Upvotes: 1

Views: 5072

Answers (3)

Dmitri Pavlutin
Dmitri Pavlutin

Reputation: 19070

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

Upvotes: 1

Mehdi
Mehdi

Reputation: 7403

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

Upvotes: 1

suvroc
suvroc

Reputation: 3062

You can use _.has method

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

Upvotes: 3

Related Questions