skip to Main Content

website works fine, but in terminal i have this warning Array.prototype.map() expects a value to be returned at the end of arrow function . How do i fix this?

i tried using forEach, there is no warning but the section doesn’t show what I wrote.

IMAGE

2

Answers


  1. You have to listen to the error. It’s because you’re not returning anything if the value’s category is not "education". You must have a return value for every element in the array for map to work. As @Andy pointed out in the comments, you have to filter the array for the category you want, and then map the items to the values you want.

    Login or Signup to reply.
  2. As Andy said, to make sure everything runs smoothly, I recommend using a .filter function first, then the .map function. To give you a better idea, here’s an example:

        Data
          .filter(val => val.category === "education")
          .map((val, id) => return (<Card key={id} ...>));
    

    If no Data meet the filter criteria, the function will return an empty array. This array will then be passed to .map, resulting in no actions being taken. Put simply, if there are no matches, there is nothing to display.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search