我有 2 個物件
const size = shoppingCart.map((el) => ({size: el.size}));
第一個是已過濾的產品串列:
const filteredProducts = [
{
description: "Material: 100% cotton\nCare instructions: Do not tumble dry, machine wash at 30 ° C, machine wash on gentle cycle",
id: "80005397",
image: {url: 'h,ttps://www.datocms-assets.com/59095/1638273249-spodnie.png'},
price: 32.8,
title: " Pier One Cargo"
}
]
第二個是選定的尺寸:
const selectedSize = [
{
size: 'L'
}
]
我必須將這 2 個物件合并為一個,如下所示:
const filteredProducts = [
{
description: "Material: 100% cotton\nCare instructions: Do not tumble dry, machine wash at 30 ° C, machine wash on gentle cycle",
id: "80005397",
image: {url: 'h,ttps://www.datocms-assets.com/59095/1638273249-spodnie.png'},
price: 32.8,
title: " Pier One Cargo",
size: 'L'
}
]
uj5u.com熱心網友回復:
您可以使用 map() 函式和擴展運算子:
const filteredProductsWithSize = filteredProducts.map((product, i) => ({
...product,
...selectedSize[i]
}));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379555.html
標籤:javascript 反应 目的
