Reputation: 63
I have three multidimensional arrays. The function findIndex, will go through one of the arrays, if I set the last line to findIndex(arrayTwo), and spit out the item and index variable telling me where the variable idName is.
Example: if I call findIndex(arrayTwo), it will console log item: 2 | index: 1 http://jsfiddle.net/vap03jfL/
The problem is that when i run the function findIndex(arrayCombine) it will only search through the first two levels of the new array set. I know I need to search one level deeper and return a value for the now top most array, but I haven't found what I need. I have also read that the way I am doing this may not be the best. to begin with.
var arrayOne = [
["a", "b", "c", "d"],
["e", "f", "g", "h"],
["i", "j", "k", "l"],
["m", "n", "o", "p"]
];
var arrayTwo = [
["Z1", "Y1", "X2", "W2"],
["V2", "U3", "T3", "S3"],
["R3", "Q3", "P3", "O3"],
["N4", "M2", "L2", "K2"]
];
var arrayThree = [
["Z", "Y", "X", "W"],
["V", "U", "T", "S"],
["R", "Q", "P", "O"],
["N", "M", "L", "K"]
];
var arrayCombine = [
[arrayOne],
[arrayTwo],
[arrayThree]
]
function findIndex(arrayArg) {
idName = "Q3";
for (var item in arrayArg) {
console.log("item: " + item);
if (arrayArg[item].indexOf(idName)) {
var index = arrayArg[item].indexOf(idName);
console.log("index: " + index);
if (index != -1) {
console.log("item found: " + item);
console.log("index found: " + index);
break;
}
}
}
}
findIndex(arrayCombine);
Any help would be greatly appreciated. Thank you.
Upvotes: 3
Views: 165
Reputation: 63
Here is the solution that I came up with. http://jsfiddle.net/vap03jfL/7/
Problem 1: the nested array was written incorrectly, I shouldn't have bracketed the variable being called in the nested array, it should have been written.
var arrayCombine = [arrayOne, arrayTwo, arrayThree];
Problem 2: I needed to parse another level.. this is a slightly different strategy but works with jagged arrays too.
function findIndex(arrayArg) { idName = "Q3";
for (var arrayId = 0; arrayId < arrayArg.length; arrayId++) {
for (var item = 0; item < arrayArg[arrayId].length; item++) {
var index = arrayArg[arrayId][item].indexOf(idName);
if (index >= 0) {
console.log("arrayId " + arrayId + " | item: " + item + " | Found Index! " + index);
}
}
}
}
Upvotes: 0
Reputation: 450
function findIndex(arrayArg, idName) {
for (var i in arrayArg) {
console.log("item: " + item);
var item = arrayArg[i];
if(item instanceof Array) {
if(findIndex(item, idName)) {
return true;
}
} else if (item.indexOf(idName)) {
var index = item.indexOf(idName);
console.log("index: " + index);
if (index != -1) {
console.log("item found: " + item);
}
}
}
}
Upvotes: 0
Reputation: 12853
You need to make your function recursive to handle a deep search.
function findIndex(arrayArg, idName) {
for (var item in arrayArg) {
if (typeof arrayArg[item] == "object" && arrayArg[item] != null) {
if (findIndex(arrayArg[item], idName)) {
return true
}
} else {
var index = arrayArg[item].indexOf(idName)
if (index != -1) {
console.log("item found: " + item);
console.log("index found: " + index);
return true;
} else {
console.log(arrayArg[item] + " != " + idName);
}
}
}
return false;
}
findIndex(arrayCombine, "Q3");
Instead of nesting if statements inside of loops inside of if statements, you can use the power of recursivity to do a deep search with no upper limit on the number of nested arrays or objects you are permitted to scan.
Upvotes: 1