check if key exists in object with lodash

拈花ヽ惹草 提交于 2020-01-13 08:28:10

问题


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/


回答1:


Try this. it's simple

_.has(dependsOn, field)

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




回答2:


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/




回答3:


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


来源:https://stackoverflow.com/questions/35651959/check-if-key-exists-in-object-with-lodash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!