user4261401
user4261401

Reputation:

find specific property inside array of object

I use the following code and I want to find if path is equal to some key I try with the following loops without success,what am I doing wrong here?

I need to find if key is exist

https://jsfiddle.net/0ukL2jxh/

var obj = {
    "prov": [{
        "save": {
            "key": "aaa",
            "method": "sa"
        },
        "delete": {
            "key": "bbb",
            "method": "del"
        }
    }]
};

var obj2 = {
    "prov": [{
        "save": {
            "key": "ccc",
            "method": "sa"
        },
        "delete": {
            "key": "ccc",
            "method": "del"
        }
    }]
};

var myArray = [];
myArray.push(obj);
myArray.push(obj2);

for (var i = 0; i < myArray.length; i++) {
    var configObj = configs[i];

    for (var j = 0; configObj; j++) {
        var prov = configObj[j];

        for (var x = 0; prov; x++) {
            var obj = prov[x];

            for (var y = 0; obj; y++) {
                  if (obj.key === 'aaa')
                      exit;
            }
        }
    }
}
}

Upvotes: 1

Views: 536

Answers (1)

Veverke
Veverke

Reputation: 11338

What about mapping your array objects into keys only (extracting the keys from your objects), then doing a simple array search for a given element ?

var keys = myArray.map(function(e, i){
   return e.prov[0].save.key;
});

// keys = ["aaa", "ccc"]

// will give you the index of the element, -1 if does not exist
var index = keys.indexOf("ccc");

in your example, you would do

if (index > -1)
   exit; // although I do not recognize such javascript statement :-)

Update:

Well, after some additional explanations from your side, the problem became clearer.

I think this is what you need:

function f(array, keyName, keyValue) {
   function findKey(obj) {
       var keyFound = false;

       for(var property in obj) {
          if (property == keyName && obj[property] == keyValue) {
             return true;
          }
          else if(typeof(obj[property]) == "object") {
            keyFound = findKey(obj[property], property);
            if (keyFound)
               return keyFound;
          }
          else {
            if (obj == keyValue)
               keyFound = true;
          }
       }       


       return keyFound;
   }
   var keyFound = false;
   for (var i = 0; i < array.length; i++) {
       keyFound = findKey(array[i]);
       if (keyFound)
          break;
   }

   return keyFound;
}

//usage:
//f(myArray, "delete", "method") -> false
//f(myArray, "keydasd", "aaa") -> false
//f(myArray, "key", "aaa") -> true
//f(myArray, "method", "del") -> true

function 'f' Parameters:

obj => your array containing objects

keyName => the name of the key to search for a matching value

keyValue => the value of the key...

Just for the record: Thanks to you I learned that you could make a generic solution for this problem, creating a function like

function f(obj, objPath, keyValue)

where you objPath can be whatever path to the key you want to search for a matching value, in the form of a string. Then using eval, you can access obj's matching value

eval (obj[objPath])

Here's an example of such a thing:

var o = { 
   a: 1,
   b: {
     c: [
       {
       x: "xxx",
       y: 1205
      },
      {
       x: 1020,
       y: 8274
      }
     ],
     d: 1000
   }
}

var p1 = "o.b"
var p2 = ".c[0].x"

eval(p1 + p2)
//result: "xxx"

Upvotes: 2

Related Questions