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/
Lodash _.has() Method The _.has() method is used to check whether the path is a direct property of object or not. It returns true if path exists, else it returns false.
There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.
Not every Lodash utility is available in Vanilla JavaScript. You can't deep clone an object, for example. That's why these libraries are far from obsolete. But if you're loading the entire library just to use a couple of methods, that's not the best way to use the library.
The _. get() function is an inbuilt function in the Underscore. js library of JavaScript which is used to get the value at the path of object. If the resolved value is undefined, the defaultValue is returned in its place. Syntax: _.get(object, path, [defaultValue])
Try this. it's simple
_.has(dependsOn, field)
it returns true if the RHID
key exist in dependsOn. in above case it returns false
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With