Javascript 使用 lodash 检查对象中是否存在键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35651959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:04:38  来源:igfitidea点击:

check if key exists in object with lodash

javascriptarraysobjectlodash

提问by Leonel Matias Domingos

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

我需要 lodash 的帮助,因为我不懂函数式编程,而 lodash 对对象/数组操作非常有帮助。

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

我需要搜索对象内的对象,如果键存在则返回 true。我已经设置了一个 jsfiddle。感谢您的帮助。

    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/

https://jsfiddle.net/88gwp87k/

回答by Fawad Mukhtar

Try this. it's simple

尝试这个。这很简单

_.has(dependsOn, field)

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

如果RHID密钥存在于dependsOn,则返回true 。在上述情况下,它返回 false

回答by Narendra CM

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/

更新了你的小提琴:https: //jsfiddle.net/88gwp87k/1/

回答by stasovlas

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