I'm struggling with getting access to the data I need and wondering if somebody can please advise.
I have data that looks similar to this:
Order: [
{
id: 1,
date: 2020-07-21,
OrderLines: {
OrderLine: [
{orderlineId: 1, price: 20},
{orderlineId: 2, price: 25}
]
}
},
{
id: 2,
date: 2020-07-21,
OrderLines: {
OrderLine: [
{orderlineId: 3, price: 10},
{orderlineId: 4, price: 15}
]
}
}
]
How can I get access so that I can .map() the OrderLine Array?
I've gone with this, which kinda works but fails with an error part way through:
Order.map(order => {
order.OrderLines.OrderLine.map(line => console.log(line))
})
The above fails with orderitems.jsx:10 Uncaught TypeError: order.OrderLines.OrderLine.map is not a function
What I'm trying to achieve:
I'm using this in a nextjs(react) app and simply need to be able to list out each object from the OrderLine array to the page.
So the issue appears to be that when there is only one item in the OrderLine array, that array becomes a single object. I shall update my example data.
Example:
Order: [
{
id: 1,
date: 2020-07-21,
OrderLines: {
OrderLine: [
{orderlineId: 1, price: 20},
{orderlineId: 2, price: 25}
]
}
},
{
id: 2,
date: 2020-07-21,
OrderLines: {
OrderLine: [
{orderlineId: 3, price: 10},
{orderlineId: 4, price: 15}
]
}
},
{
id: 3,
date: 'Its made up'
OrderLines: {
OrderLine: {orderlineId: 5, price: 5}
}
}
]
Will solve with if/else
id:2OrderLinesyntax error. second object{orderlineId: 4, price: 22}does not have a closing brace afterprice:22. That is why map is failing half way through.{ [ } ]<-- That's incorrectorder.OrderLines.OrderLine.mapthis may happen if the OrderLine isn't an array, so I advise you before doing the map, verify iforder.OrderLines.OrderLine.length > 0