I have two very long arrays containing "image names" and "image files". The first is the actual name of the images, and the second is just the file name. For instance:
picturenames[0] = '0 - zero'; picturenames[1] = '1 - one'; picturenames[2] = '1 o\'clock'; ... picturefiles[0] = 'numbers-zero.jpg'; picturefiles[1] = 'numbers-one.jpg'; picturefiles[2] = 'time-1.jpg'; ...
I have about 1000 elements in each array in several languages โโ(image files are always the same). I โrecycleโ these arrays from the previous application to save time and not rewrite everything again.
Desired functionality : using user input in a text box, I want to filter out the picturenames array and then show the corresponding picturefiles image.
The problem I am facing : when filtering an array of picturenames I lose the index and I cannot "reach" the image file name.
This is the code that I use to filter the picturenames array.
var matches = picturenames.filter(function(windowValue){ if(windowValue) { return windowValue.indexOf(textToFindLower) >= 0; } });
What would be the best way to do this?
UPDATE : Ahmed's solution is the best, but for time reasons and minor performance issues, I just use the for loop to search the array as follows:
var matchesCounter = new Array(); for (i = 0; i < picturenames.length; i++) { if (picturenames[i].indexOf(textToFindLower) >= 0) { matchesCounter.push(i); } } console.log(matchesCounter); for (i = 0; i < matchesCounter.length; i++) { console.log(picturenames[i]); console.log(picturefiles[i]); }
source share