[SalesForce] Is it possible to pass parameter to javascript method from template in LWC

I'm iterating through the list of objects, i want to render the class name based on the one attribute from the object.
Here is my sample js:

<template for:each={tempList} for:item="tempItem">
    <div key={tempItem.label}>
        <span class={testMethod.isActive} >
            {tempItem.label}
        </span>
    </div>
</template>

Best Answer

You could define a getter that processes the list and adds additional data:

export default class Foo extends LightningElement {
  ...

  @track
  activeIndex = 0;

  get processedList() {
    return this.tempList.map((item, index) => {
      const { label } = item;
      const computedClass = index === this.activeIndex ? 'active' : '';
      return {
        label,
        computedClass,
      };
    });
  }
}
<template for:each={processedList} for:item="item">
  <div key={item.label}>
    <span class={item.computedClass}>{item.label}</span>
  </div>
</template>
Related Topic