This a very simple question. I want to map and form an array given another array. then I would like to remove the duplicated values.
this is what I did:
let status = listHotels.map(hotel => {
return hotel.status
})
const x = status.filter((v, i) => (
status.indexOf(v) === i
));
It works. But I would like a solution that doesn't involve writing two blocks of code. I tried this:
let status = listHotels.map(hotel => {
return hotel.status
}).filter((v, i) => (
status.indexOf(v) === i
));
But it didnt work. It says
Cannot read property 'indexOf' of undefined
Does anyone know a workaround this?
statuswill only be assigned once all chained methods have executed, with the result of the last chained method. So you attempting to accessstatusinside a chained method before it has been assigned.array.indexOf(v) === iwill always return true...