I have simple CLI appliciation where user can specify category all individual object which will import to his project (similar as lodash custom builds)
To avoid duplicate data i create a object structure like:
{
"front": {
"subCategories": [{
"name": "front1",
"value": false
}, {
"name": "front2",
"value": true
}]
},
"other": {
"subCategories": [{
"name": "other1",
"value": false
}, {
"name": "other2",
"value": false
}]
}
}
and from user input i get
- categorie/s name, e.g. string
front
(but it can list all categories as well) - or alternativly array of objects names, e.g.
['front1', 'other2']
Based on this input i have to figurate out if atleast one value is true
. Just to note issue is not to know if user send category or individual objects, i know that from input parser :)
So i need to write 2 function which
- For each categories user pass to script (front, other in this example) check if atleast one value in any subcategories is
true
and if yea stop executing loop to avoid useless performance usage. - Loop over all subcategorieis no mather parent and check if atleast single value is
true
where name is from array of object names from input e.g. ['front1', 'other2']
What i did try:
I can imagine that if I set objValue=false;
as a default value and after that pass subCategories object to code similar as one below inside of loop may work:
var objValue = _.result(_.find(myObj, function(obj) {
return obj.name === name;
}), 'value');
and before I call this i simply ask if(objValue) {//code here}
However this is something like final step, I am somehow lost at start.
If here someone who can lead me right way how to:
- find if any value in object have value set to
true
- find in all subCategories(it can be a lot more then just 2) object if there is an object with value set to
true