I have some multi-dimensional JSON data that contains different clothing items, each clothing item containing its own array of information.
I am trying to display this as such, using ReactJS:
{Product Name}
{size 1} has {quantity 1} in stock
{size 2} has {quantity 2} in stock
{size 3} has {quantity 3} in stock
My JSON looks like this:
{
"myShirt": [
{
"size": "Small",
"quantity": 0,
},
{
"size": "Medium",
"quantity": 0,
},
{
"size": "Large",
"quantity": 0,
},
],
"myShirt2": [
{
"size": "Small",
"quantity": 0,
},
{
"size": "Medium",
"quantity": 0,
},
{
"size": "Large",
"quantity": 0,
},
],
"myShirt3": [
{
"size": "Small",
"quantity": 0,
},
{
"size": "Medium",
"quantity": 0,
},
{
"size": "Large",
"quantity": 0,
},
]
}
So for example, in this case my desired output would be:
myShirt
Small has 0 in stock
Medium has 0 in stock
Large has 0 in stock
myShirt2
Small has 0 in stock
Medium has 0 in stock
Large has 0 in stock
myShirt3
Small has 0 in stock
Medium has 0 in stock
Large has 0 in stock
I have created a React component that includes this JSON data as part of its state. This component looks like this:
class Products extends React.Component {
constructor() {
super();
this.state = {
data: [
{
"myShirt": [
{
"size_text": "Small",
"quantity": 0,
},
{
"size_text": "Medium",
"quantity": 0,
},
{
"size_text": "Large",
"quantity": 0,
},
],
"myShirt2": [
{
"size_text": "Small",
"quantity": 3,
},
{
"size_text": "Medium",
"quantity": 0,
},
{
"size_text": "Large",
"quantity": 0,
},
],
"myShirt3": [
{
"size_text": "Small",
"quantity": 3,
},
{
"size_text": "Medium",
"quantity": 0,
},
{
"size_text": "Large",
"quantity": 0,
},
]
}]
}
}
render() {
return (
<div>
{
// HOW DO I DISPLAY MY OUTPUT HERE?
}
</div>
);
}
}
My approach to displaying my output was using .map. However, the bare code I used to do this wasn't successful. It looked like this:
render() {
return (
<div>
{
this.state.data.map((product, i) =>
<p>{product.map((productData, j) =>
<span>{productData.size_text}</span>
)
}</p>
)
}
</div>
);
}
This code doesn't attempt to display the entire output as I was just testing out the waters to see if I could get anything, at least a size to display. As you can tell, I'm pretty much stuck in the render function of the component. I don't know how I would access the product name, and moreover the product size name and quantity.
How would you go about solving this to get it to look like the desired output?
Thanks!