As title suggest, I am trying to get object from an array using the index obtained from another separate array. I get error: Uncaught TypeError: venues[index].map is not a function.
venues = [
{id: 4, title: "bro"}, {id: 5, title: "np bro"}
]
cityArray = [4, 5, 5, 4, 5, 5]
Above is the venues and cityArray.
I get the index correctly. dispatches works correctly.
const Home = () => {
const { city } = useParams();
const featured = useSelector((state) => state.featuredList.featured);
const venues = useSelector((state) => state.venueList.venues);
useEffect(() => {
dispatch(listVenues())
}, [dispatch])
useEffect(() => {
dispatch(listFeatured())
}, [dispatch])
useEffect(() => {
if (city === "perth") {
setCityArray(featured?.featured_perth)
}
}, [featured, city])
return (
<GridWrapper>
{cityArray?.map((i) => {
var index = venues?.findIndex(ar => parseInt(ar.id) === i);
return (
<div>
{venues[0].map(arr =>
(
<div>
<h1>{arr.title}</h1>
</div>
)
)}
</div>
)
}
)}
</GridWrapper>
)
}
export default Home;
id. Even if you keep looking them up individually, you can simply usefindinstead offindIndexto return the object itself.