cjros
1
Is it possible to map an array to elements and use conditional rendering inside?
Something like this:
${(products.map((product) => $h`
${product.title.length > 0 && $h`
<h1 innerHTML="${product.title}"></h1>
`}
`))}
It works but it also prints the words true or false
Maybe try filtering first. After mapping it should then come the string:
${products.filter((product) => product.title.length > 0).map((product) => $h`
<h1 innerHTML="${product.title}"></h1>
`)}
1 Like
cjros
3
My example wasn’t good
I actually want to keep the products without the title
cjros
4
I would like to revisit this topic.
I have the problem in another place.
Is it possible to use an if-condition inside an array map?
Yes, it is possible:
[1,2,3].map((el) => {
if (something) return 'foo';
else return 'bar';
})