I'm trying to test something in javascript with an array and object here is what I'm trying to do:
const data = [
{
name: "Order A",
orderItems: [
{
qty: [2, 2, 2],
products: [
{ name: "Product A" },
{ name: "Product B" },
{ name: "Product C" },
],
},
],
},
];
const dataName = data.map(({ name }) => {
return name;
});
const product = data.map(({ orderItems }) => {
return orderItems.map(({ qty, products }) => {
return products.map(({ name }) => {
return name + qty;
});
});
});
console.log(`This is ${dataName} with ${product}`);
I want it to return something like A2, B2, and C2 but it returns something like A2,2,2 B2,2,2 C2,2,2 I know I did something wrong. If you need anymore clarification please let me know.
You're concatenating the whole qty array. Pass the products array index and concatenate the corresponding qty instead.
const product = data.map(({ orderItems }) => {
return orderItems.map(({ qty, products }) => {
return products.map(({ name }, i) => {
return name + qty[i];
});
});
});
console.log(`This is ${dataName} with ${product}`);
Output
This is Order A with Product A2,Product B2,Product C2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With