1

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

0

3 Answers 3

1

A simpler approach may be to reduce the object and map the array to a string like below.

let variants = [{color: "red,blue"}, {size: "m,39"}];
let result = variants.map(obj => 
  Object.entries(obj).reduce((s,[key,val]) => s+=`${key[0].toUpperCase()+key.slice(1)} - ${val}`,'')); // creates a string of the required type
result = result.join(', ');
console.log(result);

renderVariants(variants){
    return variants && variants.map(obj => 
      Object.entries(obj).reduce((s,[key,val]) => s+=`${key[0].toUpperCase()+key.slice(1)} - ${val}`,'')).join(', ');
}
Sign up to request clarification or add additional context in comments.

8 Comments

join would be more appropriate than reduce + slice off comma.
Without console.log(result), result is empty. It shows nothing on the table
you must return the result !
I am returning it. renderVariants(variants){ result = ''; if (variants != null && variants.length > 0) { let result = variants.map(obj => Object.entries(obj).reduce((s,[key,val]) => s+=${key[0].toUpperCase()+key.slice(1)} - ${val},'')); // creates a string of the required type result = result.join(', '); }else{ var result = 'null'; } return result; }
Works. Changed 'let' to 'var'
|
0

Declare message variable outside if condition

renderVariants(variants){
      var message = '';
        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;
                    message = color+size;
                }
            });
        }else{
            var message = 'null';
        }
        return message;
    }

Comments

0

You could map on the array then merge each object entry then join the array

const variants = [{
    color: "red,blue"
  },
  {
    size: "m,39"
  }
]

let v = variants.map(variant => Object.entries(variant).flat().join(' - ')).join(', ')

console.log(v)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.