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
check if key exists in object with lodash
提问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))
回答by Fawad Mukhtar
Try this. it's simple
尝试这个。这很简单
_.has(dependsOn, field)
it returns true if the RHID
key 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();