1

I have an array of products, some products may have an array of product colors, but it's not guaranteed. I am trying to map over every color of each product (if available) to give it it's own div. The piece of code I wrote works except for the product.product_colors.map() part. Any contribution or linking to resources would help. Thanks

Fetched data from api looks like this:

{
    "status": 200,
    "count": 2,
    "data": [
        {
        "product_id": 1,
        "product_name": "PRODUCT NUMBER 1",
        "product_price": "14.95",
            "product_colors": [
            {
            "color_id": 1,
            "color_name": "Velvet",
            "color_description": "A reddish-purple color"
            },
            {
                "color_id": 2,
                "color_name": "Navy",
                "color_description": "A darkish blue color somewhat similar to the deep ocean color."
            }
            ]
        },
        {
        "product_id": 2,
        "product_name": "PRODUCT NUMBER 2",
        "product_price": "8.99"
        }
    ]
}

My current code looks like this:

    return(
        <div>
            {data.map((product, index) => (
                <div key={index}>
                    <h2>{product.product_id} ---- {product.product_name}</h2>
                    <h2>{product.product_price}</h2>

                    {product.product_colors
                    ?<div><p>No colors available</p></div>
                    : product.product_colors.map((color) => (
                        <div>
                            <p>{color.color_name}</p>
                            <p>{color.color_description}</p>
                        </div>
                    ))
                    }

                    <br />
                </div>
            ))}
        </div>
    );
2
  • Looks like comparison issue. "product.product_colors" is TRUE as long as it's not undefined or null. Simple solution might be adding an "exclamation" to !product.product_colors. Commented Mar 2, 2022 at 15:02
  • @0stone0 Not for every product, but for those who do have colors it does have a product.product_colors array. I have a console.log(data) after it fetches the data from an api. And this is what it gives : ``` {product_id: 1, product_name: 'PRODUCT NUMBER 1', product_price: '12.95', product_colors: Array(6)} ``` Commented Mar 2, 2022 at 15:02

3 Answers 3

1

You've switched the conditions. You want to map over the array when product.product_colors is truthy, which is the first condition.

in other words, you want

product.product_colors ? product.product_colors.map((color) => (
                        <div>
                            <p>{color.color_name}</p>
                            <p>{color.color_description}</p>
                        </div> : <div><p>No colors available</p></div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It does seem to work just fine now, can't believe I got this wrong
1

Error is due to the second item not having a product_colors property. You need to use optional chanining operator (?.) to get rid of error you get.

product.product_colors?.map((color) => (
  <div>
    <p>{color.color_name}</p>
    <p>{color.color_description}</p>
  </div>
))

1 Comment

This works perfectly aswell! Can't believe that this had multiple simple solutions that I couldn't figure out. Thanks for the quick answer!
1

You need to ensure product.product_colors exists.

The shortest way to do so is using the optional chaining ?. syntax:

product.product_colors?.map((color, index) =>

Small example:

const data = {"status": 200, "count": 2, "data": [{"product_id": 1, "product_name": "PRODUCT NUMBER 1", "product_price": "14.95", "product_colors": [{"color_id": 1, "color_name": "Velvet", "color_description": "A reddish-purple color"}, {"color_id": 2, "color_name": "Navy", "color_description": "A darkish blue color somewhat similar to the deep ocean color."} ] }, {"product_id": 2, "product_name": "PRODUCT NUMBER 2", "product_price": "8.99"} ] };

const res = data.data.map((product, index) => {
    product.product_colors?.map((color, index) => {
        console.log(color);
    });
});

1 Comment

This works exactly how I wanted it. Perfectly explained aswell, thank you!

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.