Home > front end >  TypeError: arr.map is not a function in js, and nextjs
TypeError: arr.map is not a function in js, and nextjs

Time:06-22

I am getting this error, but the code was working now it's not working somehow.

TypeError: arr.map is not a function

Am I doing something wrong?

export const footerAdapter = (footerData) => {
  const arr = footerData[0].attributes['items']
    return arr.map((md) => {
      return {
        link: md.link,
        title: md.title
      };
  });
};

Update:

I am trying yo fetch this data:

[
    {
        "attributes": {
            "items": {
                "title": "Hello world",
                "link": "/Home",
                "__typename": "ComponentFooterFooterItems"
            },
            "__typename": "Footer"
        },
        "__typename": "FooterEntity"
    },
]

CodePudding user response:

The "TypeError: map is not a function" occurs when we call the map() method on an object that is not an array.

To solve the error, console.log the value you're calling the map() method on and make sure to only call the map method on valid arrays.

const obj = {};

// ⛔️ Uncaught TypeError: map is not a function
const result = obj.map(element => {
  return element   1;
});

You can conditionally check if the value is an array by using the Array.isArray method.

const arr = 'test';

const result = Array.isArray(arr) ? arr.map(element => element   1) : [];

console.log(result); //            
  • Related