Как разместить каждый объект с помощью axios - PullRequest
0 голосов
/ 26 мая 2020

Поскольку в моем "finalArr" много элементов, как я могу разместить каждый элемент с помощью карты. Код ниже работает нормально, если у меня есть только один элемент. Но у меня проблема, когда в 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);
  })

1 Ответ

3 голосов
/ 26 мая 2020

Используйте Array.prototype.map. Попробуйте ниже:

const orders = finalArr.map(e => ({
  name: e[0].name,
  productImage: e[0].productImage,
  price: e[0].price,
  quantity: e[1],  
}));
...