I am iterating over data and want to set element class based on value. Is there are way to check if element.Id exist? If true is not an option for me.

<template for:each={data} for:item="element" for:index="inx">
  <p class = {testClass}>


get testClass(){ 
   if(element.Id) {
       return 'someClass'
   } else {
       return 'anotherClass'

1 Answer 1


In your data object you can add a additional key value pair of class based on element Id.

You can iterate and add className key like -> data.className = data.Id ? 'idclass':'withoutIdClass'.

And you can reference that directly in your html. class = {element.className}.

Add respective css of those classes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .