I am looping through an array of objects and displaying the result in a table.
renderProducts(){
return this.state.products.map((product, index) => (
<tr key={product.id}>
<td>{product.code}</td>
<td>{product.name}</td>
<td>{product.price}</td>
<td>{this.renderVariants(product.variants)}</td>
</tr>
))
}
Now, product.variants is also an array of objects. So I need to loop through the array and assign to a variable.
This is what this array looks like,
variants: Array(2)
0: {color: "red,blue"}
1: {size: "m,39"}
I want to loop through this array and show the result like this,
Color - red, blue, Size - m, 39.
This is what I am trying to do -
renderVariants(variants){
if (variants != null && variants.length > 0) {
variants.map((variant, index) => {
for(var key in variants) {
var color = variants[key].color;
var size = variants[key].size;
var message = color+size;
}
});
}else{
var message = 'null';
}
return message;
}
This only returns 'null' and returns empty for the color and size. How do I display the results the way I earlier said. Also, for example, the array may look like this too:
variants: Array(1)
0: {size: "t,u"}
So the result would be Size - t,u. How do I check if size, color is present before displaying their values?
Thanks