Skip to content
Advertisement

Uncaught TypeError: work.filter is not a function

I can’t seem to figure out why I am getting this error

Uncaught TypeError: work.filter is not a function
    at computing_experience.js:7
    at Array.map (<anonymous>)
    at computing_experience.js:5

when running the filter method on an array (imported from a separate file) anyone have any ideas?

import workExpArray from "../Arrays/workExpArray.js";

const workx = document.querySelector(".workexp");

const newArticle = workExpArray.map((work) => {
  let arrayItem = work
    .filter(function (workexp) {
      if (workexp.industry === "Computing") {
        return true;
      } else if (workexp.industry != "Computing") {
        return false;
      }
    })
    .map((workexp) => {
      let workExpArticle = document.createElement("article");
      workExpArticle.classList.add("workexp__article");
      workExpArticle.setAttribute("id", workexp.id);
      if (typeof workexp.secondaryRole === "string") {
        workExpArticle.innerHTML = `
              <SOME HTML HERE>
          `;
      } else {
        workExpArticle.innerHTML = `
              <MORE HTML HERE>
          `;
      }
      return workExpArticle;
    });
  if (workexp.industry === "Computing") {
    arrayItem.forEach((workexp) => {
      workx.append(workexp);
    });
  }
});

This is my first question on here and I’m reasonably new to writing code, so you may need more context 🙂 let me know if so!

Thanks for any help! Ollie

Advertisement

Answer

error occurs when you call work.filter it seems to me that the work is not an array due to which there is no function named filter to call, that’s why error is thrown. Make sure your imported array is 2 dimensional meaning it’s elements are also arrays

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement