HOW DO I LOOP THROUGH AN ARRAY OF ARRAYS and do something based on a match? I want to:
- search each array at a particular index[position] to see if it matches the term inserted into localStorage,
2. when match is found, pop() the matching index[7] entry (which is “hide”),
3. then push(“show”) to the same array, effectively replacing the popped entry (turning it to “show”).
Once that is over, I loop through the arrays again to:
- find each array with index[7] containing “show,”
2. grab the name of the model in index[0] and its page URL in index[2],
3. output the results in a div for the user, making the model name clickable.
In effect, the buttons filter the list of models and only display the ones having “show.”
After this, new buttons appear to show new criteria to filter it further, this time changing “show” to “hide” to filter them out.
PROBLEM:
Console.log keeps showing all the elements of the array instead of the specific indexes I am looking for.
So my looping script is wrong, and I don’t know how to put it right. That is where I need help.
There are no more errors in Chrome Tools as of this posting. Console is dutifully outputting the results of my code.
Here is the array of arrays:
< script>
var model = [
["Car1", "thumb_url", "page_url", "kit", "elect", "truck", "1:10", "hide"],
["Car2", "thumb_url", "page_url", "kit", "elect", "buggy", "1:10", "hide"],
["Car3", "thumb_url", "page_url", "rtr", "elect", "truck", "1:10", "hide"],
["Car4", "thumb_url", "page_url", "rtr", "elect", "truck", "1:10", "hide"],
["Car5", "thumb_url", "page_url", "rtr", "elect", "truck", "1:10", "hide"],
["Car6", "thumb_url", "page_url", "kit", "elect", "buggy", "1:8", "hide"],
["Car7", "thumb_url", "page_url", "kit", "nitro", "buggy", "1:8", "hide"],
["Car8", "thumb_url", "page_url", "kit", "elect", "truck", "1:8", "hide"],
["Car9", "thumb_url", "page_url", "kit", "nitro", "truck", "1:8", "hide"],
["Car10", "thumb_url", "page_url", "rtr", "elect", "buggy", "1:10", "hide"],
["Car11", "thumb_url", "page_url", "rtr", "elect", "buggy", "1:18", "hide"],
["Car12", "thumb_url", "page_url", "rtr", "elect", "truck", "1:18", "hide"]
];
< /script>
SEQUENCE OF PAGE:
- Two buttons call functions that correctly put their value of rtr or kit into localStorage (console.log confirms this works).
- Then they call the looping section, looking in index[3] for a match. Intent of the code is to display only those models that match the button selection.
- So if button for “kit” was selected, then we’ll see Car1, Car2, and Car6-Car9 listed in the div.
Here is the looping section:
< script>
function getList(){
[ SNIPPED - code to create the div for output goes here ]
// from http://www.dyn-web.com/javascript/arrays/multidimensional.php
// look for "results" match (kit or rtr) and change "hide" to "show" where matched
for (var i=0, len=model.length; i" + model[i][0] + "");
document.getElementById("carlist").appendChild(modList);
// document.getElementById("carList").innerHTML += "" + model[i][0] + ""; // list model name
}
}
}
}
< /script>
Where am I going wrong in my matching/displaying code?