Leonel Matias Domingos
Leonel Matias Domingos

Reputation: 2040

check if key exists in object with lodash

I need help with lodash cause i dont understand functional programming and lodash is very helpfull with object/arrays operations.

I need to search objects inside object and return true if key exists. I've setup a jsfiddle. Apreciate your help.

    var dependsOn={
      "Cadastro": {
        "RHID": "RHID"
      },
      "Agregados":{
        "CD_DOC":"CD_DOC"
      }
      "Documentos":{
        "RHID":"CD_DOC"
      }
    }
    var field='RHID'

alert(_.contains(_.keys(dependsOn), field))

https://jsfiddle.net/88gwp87k/

Upvotes: 31

Views: 72294

Answers (3)

Fawad Mukhtar
Fawad Mukhtar

Reputation: 880

Try this. it's simple

_.has(dependsOn, field)

it returns true if the RHID key exist in dependsOn. in above case it returns false

Upvotes: 56

stasovlas
stasovlas

Reputation: 7406

_.chain(dependsOn).findKey(field).isString().value();

Upvotes: 1

Narendra CM
Narendra CM

Reputation: 1426

try this

var dependsOn={
  "Cadastro": {
    "RHID": "RHID"
  },
  "Agregados":{
    "CD_DOC":"CD_DOC"
  },
  "Documentos":{
    "RHID":"CD_DOC"
  }
}
var field='RHID'

alert(_.some(dependsOn, function(o) { return _.has(o, field); }));

Updated your fiddle: https://jsfiddle.net/88gwp87k/1/

Upvotes: 12

Related Questions