0

I am currently mapping over some data and I would like to create a unique className for each result being returned specifically for the test.typical_dq_domain.

For example below shows what data is being returned... With the value displaying Uniqueness would like to have a className called Uniqueness etc.

enter image description here

 {unitTestTemplates.map(test =>
          <div className='Table CreateUnitTestsGrid' style={{cursor: 'pointer'}}>
            <div>{test.test_type}</div>
            <div>{test.typical_dq_domain}</div>
          </div>
        )}
        </div> 

2 Answers 2

1

You can use template strings to create dynamic classes based on values in any variables you're using! In this case, if the typical_dq_domain property contains the string that you want to be your class name, you can do something like this.

{unitTestTemplates.map((test, index) =>
   <div className={`Table CreateUnitTestsGrid ${test.typical_dq_domain}`}>
      <div>{test.test_type}</div>
      <div>{test.typical_dq_domain}</div>
   </div>
)}

Then, if test.typical_dq_domain had a value of "VALIDITY", for example, that div would have classes "Table", "CreateUnitTestsGrid", and "VALIDITY" applied to it.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks, just getting an unexpected token with the } on the end. Any idea!?
Sorry, which } are you referring to? The one at the end of the whole code snippet, or the one at the end of the className definition?
Ah that's my bad, I had an extra paren in there at the top :) I fixed it, it should be fine now.
Thank you, amazing! Exactly what I was looking for.
Awesome, glad I could help!
1

Please have a look I hope it's helpful. Thanks

{unitTestTemplates.map((test,index) =>
          <div className={`Uniqueness_${index}`} style={{cursor: 'pointer'}}>
            <div>{test.test_type}</div>
            <div>{test.typical_dq_domain}</div>
          </div>
        )}
        </div> 

4 Comments

Ah thanks, what if I wanted the rest of the typical_dq_domain values to have a unique class. Like for instances consistency would then have a class Name of consistency
Welcome, Please have a look https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector I hope You will get your answer.
You need to use querySelector to select a particular row on behalf of a class
@tjhunt Please mark as an accepted answer for other's developers easily identify the right one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.