我正在嘗試合并一個products
相同的陣列,order_id
但還要添加第二個products
陣列的所有物件。這是我的訂單示例。
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [
{
"name": "product xyz"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
},
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [
{
"name": "product ghj"
}
],
},
]
這就是我想把它變成
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs & goods",
"products": [
{
"name": "product xyz"
},
{
"name": "product ghj"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
}
]
uj5u.com熱心網友回復:
將您的陣列轉換為一個物件,其中鍵等于order_id
每個訂單的屬性。這樣,您只需查找order_id
密鑰即可輕松檢查是否有任何重復項。
對于order_type
我建議您使用字串陣列。假設您有 5 個不同型別的重復項,那么事情將如下所示"drugs & prescriptions & goods & clothing & footwear"
。
const orders = [{
"order_details": {},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [{
"name": "product xyz"
}],
},
{
"order_details": {},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [{
"name": "product abc"
}],
},
{
"order_details": {},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [{
"name": "product ghj"
}],
},
];
function combineOrders(orders) {
const orderIndex = {};
for (const { order_id, order_type, products, ...rest } of orders) {
if (typeof orderIndex[order_id] !== 'undefined') {
const order = orderIndex[order_id];
const orderType = Array.isArray(order.order_type)
? [...order.order_type, order_type]
: [order.order_type, order_type];
const orderProducts = [...order.products, ...products];
orderIndex[order_id] = {
...rest,
order_id,
order_type: orderType,
products: orderProducts
}
} else {
orderIndex[order_id] = {
order_id,
order_type,
products,
...rest
}
}
}
return Object.values(orderIndex);
}
const newOrders = combineOrders(orders);
console.log(newOrders);
uj5u.com熱心網友回復:
嘗試這個:
const orders = [
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "drugs",
"products": [
{
"name": "product xyz"
}
],
},
{
"order_details": {
},
"order_id": "2",
"order_status": "Pending",
"order_type": "Prescriptions",
"products": [
{
"name": "product abc"
}
],
},
{
"order_details": {
},
"order_id": "1",
"order_status": "Pending",
"order_type": "goods",
"products": [
{
"name": "product ghj"
}
],
},
];
const result = [];
for (let order of orders){
const exist = result.find(e => e.order_id === order.order_id);
if(exist){
exist.products.push(...order.products);
if(!exist.order_type.split(' ').some(e => e === order.order_type))
exist.order_type = ' & ' order.order_type;
}
else result.push(order);
}
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/503650.html
標籤:javascript 节点.js 反应 打字稿 ecmascript-6
上一篇:路由端點不會被ReactRouterV6和ContextAPI命中
下一篇:如何在Reactjs簡單的測驗應用程式中正確處理useStates和useEffects,該應用程式將所有內容渲染兩次?