Home > Software engineering >  Cannot read property 'split' of undefined Array.map
Cannot read property 'split' of undefined Array.map

Time:03-27

I am making and API REST and take the data that is important for me. And i want to split the property @type to get the last value in the example "CuentacuentosTiteresMArionetas" of it and i am doing it like this

titulos = url.data["@graph"].map(titulo => ({

        titulo: titulo.title,
        tipo: (titulo["@type"]).split('/').pop(),

Of the following data

data It gives me the error Cannot read property 'split' of undefined Array.map.

When I try it apart with a let with that value it works correctly but not in the map

If someone could help me. Thanks

CodePudding user response:

You need to apply split within a return

titulos = url.data["@graph"].map(titulo => ({
  return {
    titulo: titulo.title,
    tipo: (titulo["@type"]).split('/').pop(),
  }
})
    

CodePudding user response:

I have solved it like this and works:

titulos = url.data["@graph"].map(titulo => ({

titulo: titulo.title,
tipo: titulo["@type"] ? titulo["@type"].split('/').pop() :null,

})

  • Related