Array.prototype.filter() expects a value to be returned at the end of arrow function
39 e.preventDefault();
40 search.addEventListener("change", (e) => {
41 userInput = String(e.target.value).toLowerCase().trim();
42 let FilteredTimeZoneArray = allTimezonesArray.filter((eachTimeZone) => {43 let eachTimeZoneString = String(eachTimeZone.textContent).toLowerCase();
44 if (eachTimeZoneString.includes(userInput)) {
45 eachTimeZone.parentElement.style.display = "block";
Description
Array
has several methods for filtering, mapping, and folding.
If we forget to write return statement in a callback of those, it's probably a mistake.
If you don't want to use a return
or don't need the returned results, consider using .forEach
instead.
Bad Practice
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
return;
}
});
Recommended
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
const bar = foo.map(node => node.getAttribute("id"));