javascript - find specific property inside array of object -


i use following code , want find if path equal key try following loops without success,what doing wrong here?

i need find if key 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);  (var = 0; < myarray.length; i++) {     var configobj = configs[i];      (var j = 0; configobj; j++) {         var prov = configobj[j];          (var x = 0; prov; x++) {             var obj = prov[x];              (var y = 0; obj; y++) {                   if (obj.key === 'aaa')                       exit;             }         }     } } } 

what mapping array objects keys (extracting keys objects), doing simple array search given element ?

var keys = myarray.map(function(e, i){    return e.prov[0].save.key; });  // keys = ["aaa", "ccc"]  // give index of element, -1 if not exist var index = keys.indexof("ccc"); 

in example, do

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

update:

well, after additional explanations side, problem became clearer.

i think 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;    (var = 0; < 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 => array containing objects

keyname => name of key search matching value

keyvalue => value of key...

just record: learned make generic solution problem, creating function like

function f(obj, objpath, keyvalue) 

where objpath can whatever path key want search matching value, in form of string. using eval, can access obj's matching value

eval (obj[objpath]) 

here's example of such 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" 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -