1

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;
 }
 }
 }
}
}
asked Aug 5, 2015 at 11:38
0

1 Answer 1

2

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"
answered Aug 5, 2015 at 11:43
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks but the save is hardcoded and I've also delete and it can be other string,so how can I adopt your example?
turning this code into a function that receives a parameter. Will update my answer
I test it and the only issue is with this statment if (property == keyName && obj[property] == keyValue) { return true; }
in this case when the value is found why not to exit all the function with true, when the value (keyValue)is found exit all the process with true...Thanks for the support!
Do not understand what the issue is - there is a return true there, meaning it leaves findKey() with true (as you want) ? Keep in mind that findKey will be in the middle of recursive chain calls to findKey() (meaning, upon leaving the function with true, it has to go all the way up in the recursive chain, to the outermost/initial call so the process ends) Are there specific cases when the function return incorrect values ? could you post them ?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.