DEV Community

Discussion on: Vanilla JavaScript live search

Collapse
stexx profile image
Stefan Breitenstein

Hi, nice little example :)
Instead of the forEach loop you could use a map function, so that you don't need the results closure.

const showList = () => {
    results.innerHTML = "";

    const newElements = data
    .filter((item) => {
      return (
        item.country.toLowerCase().includes(search_term) ||
        item.name.toLowerCase().includes(search_term)
      );
    })
    .map((e) => {
      const li = document.createElement("li");
      li.innerHTML = `<i>Name:</i> ${e.name}  || <i>Country:</i> ${e.country}`;
       return li;
    });
  results.append(...newElements);
};
Enter fullscreen mode Exit fullscreen mode
Collapse
dailydevtips1 profile image
Chris Bongers Author

Ah your right, missed that part, thanks for the addition! 💜