I have created an if statement that filters the array based on user input, acting as a search bar for the array.
var filter = userInput.value.toUpperCase();
for (var i = 0; i < myArrayElements.length; i++) {
if (myArrayElements.toUpperCase().indexOf(filter) != -1) {
myArrayElements[i].style.display = 'list-item';
} else {
myArrayElements[i].style.display = 'none';
}
}
Now, I am looking to determine the length of the filtered array and use it in the global scope.
I attempted to do this within the if statement:
var arrayLength = myArrayElements[i].length;
return arrayLength;
and
console.log(myArrayElements[i].length);
However, both attempts resulted in console errors.
Edit: Nina Scholz's response provided the correct length of the array and presented a great example. Additionally, other responses suggesting the use of .filter() are also valuable. Thank you all!