How to pass a function only after an entire array has been read and not for each element?
How to pass a function only after an entire array has been read and not for each element?
I'm trying to run a function when reading an array, but instead of running the function to each element, which I'm currently using forEach for, I want to make the script to read the entire array and then pass a function.
forEach
What I'm trying to say is this:
data.forEach(movie =>
// Log each movie's title
//console.log(movie.title);
// Check if the userWord matches
if (movie.title.toUpperCase().includes(userWord.toUpperCase()))
alert("YES");
else
alert("NO").
);
Let's say my array is: array = ["Aa", "Ba", "Ca", "Da"];
array = ["Aa", "Ba", "Ca", "Da"];
If the user enters: a, then the script would alert("YES") four times, and I want to make it alert just once, at the end of the iteration.
alert("YES")
For the same example, if the users enters: B, then the script would first alert("NO") once, then alert("YES"), then alert("YES") 2 times, and I want to make it just alert("YES")once, in the end.
alert("NO")
alert("YES")
alert("YES")
alert("YES")
Finally, if the users enters: Ferrari, then the script would alert("NO") four times, and I just want it to alert("NO") at the end.
alert("NO")
alert("NO")
I tried to make it very clear here, that's why the three "cases" of what is happening.
In the end, I want to know if there is a method that is kinda the opposite of the forEach or the common for. Something that just executes the function after reading the entire array.
forEach
for
Exactly @ibrahimmahrir and I already can do it, but i'm having problem in printing an answer for the user.
– Victor Rhéa
Sep 9 '18 at 18:06
Do you really need to loop the entire array? Can't you just stop at the first matched element if it exist?
– ibrahim mahrir
Sep 9 '18 at 18:07
@ibrahimmahrir actually I want to show if there are matches AND what matches were found. But I think that with the
some() structure that people answered down below, I can achieve it.– Victor Rhéa
Sep 9 '18 at 18:45
some()
4 Answers
4
if you want a list of the results, you should store the names in an array and outside of the loop - print.
see below:
Non-loop method:
data = ["test","hello", "hello1"];
search = "lo";
const matches = data.filter(movie => movie.includes(search));
alert(matches) //array of only matches - only has hello and hello 1
I don't know if there are performance gains against a loop... I suppose you could do a side by side comparison on a large dataset
Loop method:
var matches = "";
data.forEach(movie =>
// Check if the userWord matches
if (movie.title.toUpperCase().includes(userWord.toUpperCase()))
matches += movie.title + "<br> ";
);
if (matches.length > 0)
document.getElementById("results").innerHTML = matches;
else
alert("No match found");
You'll see more customization on the first loop, but I think filtering data first is the way to go.
ha - I was editing as you typed :) changed
– Captain Comedy
Sep 9 '18 at 18:15
What language is this code written? Javascript is not a typed language. Did you mean
var matches = "";?– ibrahim mahrir
Sep 9 '18 at 18:20
var matches = "";
yes my bad, I'm writting .net at the same time
– Captain Comedy
Sep 9 '18 at 18:26
I think that this is exactly what I'm looking for. I did not think about storing it in another array, but now you and other people gave this hint. Amazing, thanks!
– Victor Rhéa
Sep 9 '18 at 18:48
And with that, I can now post comments :) There might be a non-loop way of doing it as loops can be slow for large data-sets...
– Captain Comedy
Sep 9 '18 at 18:51
If I understand the question correctly, I believe you want to execute something if a predicate matches at least one of the items in the array (correct me if I'm wrong). For that you can use the some method like so:
some
if (movies.some((movie) => movie.toUpperCase().includes(userWord.toUpperCase())))
alert('YES');
else
alert('NO');
I think closest what you can get is some. Here is example
let data = ["test","hello", "hello1"];
let userWord = "el";
let res = data.some(movie => movie.toUpperCase().includes(userWord.toUpperCase()));
console.log(res) // prints true- means there was at least one "match", so you can alert
You could use the filter array function to filter array elements that match your criteria and then finally alert only if a match is found. Using this method you could get all the elements that have matched to your userWord. That is, the match array will contain all the possible matches.
filter
userWord
match
var data = ['Aa', 'Bb', 'Cc', 'Dd'];
var flag = false;
var userWord = 'F'
var match = data.filter(movie => movie.indexOf(userWord) !== -1);
if(match.length)
console.log('YES');
else
console.log('NO');
Storing it in another array is the catch here for me, thank you!
– Victor Rhéa
Sep 9 '18 at 18:49
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you want to check if there is a movie that matches or not?
– ibrahim mahrir
Sep 9 '18 at 18:05