I have a situation that so far I have not been able to find a satisfactory solution for. Below is the code on a high level.
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9],
o = {a:1, b:2, c:3, d:10, e:11, f:12, g:7, h:8, i:9};
function matched(i, j) {
return a[i]===o[j];
}
for (var i=0; i<a.length; ++i) {
for (var j in o) {
if (matched(i, j)) console.log(a[i]);
}
}
I have an array and an object. I'm looping through the array, then the object, to find a match via the function matched() which returns a boolean true or false. If the condition is true, then I log the array item. If you run the code right now (https://jsfiddle.net/thdoan/0tubbokj/), you should see numbers 1-3 and 7-9 outputted to the console.
What I'm trying to do is to output the numbers with a one-second delay in between each number. I know how to introduce a delay in between each loop iteration, but I only want to add the delay for the numbers that are printed (i.e., when matched() returns true).
Clarification: My current solution, which I'm not satisfied with, is to save the matched items to a separate array and iterate over that array with a delay, but I'm looking for a solution that does not require creating a new array.
for()with a recursive function. If you can't, you cansetTimeout(function() { console.log(result); },1000);but it's not fine.