I'm mapping a little block of project objects for a set of footer links and I want each project name to be followed by a | EXCEPT the last one. Here's the code.
const projects = [
{
lastOne: false,
project: "Kuma",
link: "",
image: "./kuma.png",
},
{
lastOne: false,
project: "Rin Starseed",
link: "",
image: "./star.png",
},
{
lastOne: true,
project: "Sea Harvest",
link: "",
image: "./fish.png",
},
];
Then the logic in the footer component
function ProjectLinks({ link, project, lastOne }) {
return (
<a href={link} target="_blank" style={{ color: "white" }}>
{lastOne ? project + " | " : project}
</a>
);
}
It evaluates to false, only lists the project names no |
Your if statement is the wrong way around, it should be:
You're probably not passing the props to
ProjectLinkswherebylastOneis a falsy value and so the|isn't shown.You could spread the complete object to pass all the props:
Demo:
But this is not really scalable, since you'll need to loop over the items in the projects array, you can use the iterator of the loop (
map()in the examples) to check if that is the last item in the list:Demo: