如何使用 axios post 每个对象
How to post every object using axios
由于我的 "finalArr" 有很多项目,我如何 post 每个项目都使用地图。如果我只有一项,下面的代码可以正常工作。但是当 "finalArr" 中有更多项目时,我遇到了问题。
const orders = [{
name: finalArr[0][0].name,
productImage: finalArr[0][0].productImage,
price: finalArr[0][0].price,
quantity: finalArr[0][1],
}, ];
const customerData = {
username,
orders,
};
axios
.post("http://localhost:5000/api/cart", customerData)
.then((res) => {
console.log(res.data);
})
使用Array.prototype.map
。尝试以下:
const orders = finalArr.map(e => ({
name: e[0].name,
productImage: e[0].productImage,
price: e[0].price,
quantity: e[1],
}));
由于我的 "finalArr" 有很多项目,我如何 post 每个项目都使用地图。如果我只有一项,下面的代码可以正常工作。但是当 "finalArr" 中有更多项目时,我遇到了问题。
const orders = [{
name: finalArr[0][0].name,
productImage: finalArr[0][0].productImage,
price: finalArr[0][0].price,
quantity: finalArr[0][1],
}, ];
const customerData = {
username,
orders,
};
axios
.post("http://localhost:5000/api/cart", customerData)
.then((res) => {
console.log(res.data);
})
使用Array.prototype.map
。尝试以下:
const orders = finalArr.map(e => ({
name: e[0].name,
productImage: e[0].productImage,
price: e[0].price,
quantity: e[1],
}));