Andurit Andurit - 3 months ago 9
Javascript Question

How to correctly search inside of javascript Object

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


  1. 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.

  2. 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


Answer

Here you go, try this:

var data = {
    "front": {
        "subCategories": [{
            "name": "front1",
            "value": false
        }, {
            "name": "front2",
            "value": false
        }]
    },
    "other": {
        "subCategories": [{
            "name": "other1",
            "value": false
        }, {
            "name": "other2",
            "value": true
        }]
    }
}

function searchSubCategories(obj, subCats)
{
  var ret = false;
  for (var property in obj)
  {
      for (var i=0; i <obj[property].subCategories.length; i++){
        for (var x =0; x < subCats.length; x++)
        {
          ret = (obj[property].subCategories[i].name === subCats[x] && obj[property].subCategories[i].value === true);
          if (ret)
            break;
        }
        if (ret)
            break;
      }
    if (ret)
      break;
  }
  return ret;
 }

function searchCategory(obj, category){
  var ret = false;
  for (var i=0; i <obj[category].subCategories.length; i++)
  {
    ret = obj[category].subCategories[i].value;
    if (ret)
      break;
  }
  return ret;
}

console.log(searchSubCategories(data, ['front1', 'other2']));
console.log(searchCategory(data, 'other'));
console.log(searchCategory(data, 'front'));