既存のコードのほとんどの問題の根源は、同じオブジェクト(this.locations
とthis.orderPickupTimes
の配列)を再利用していることです。createNewItem
を渡すと、何度も繰り返します。あなたのコード内にこれらの行を追加してください:
// instead of var location = this.locations[j];
var location = Object.assign({}, this.locations[j]);
// and within the internal loop
// instead of var order_pickup_time = this.orderPickupTimes[k];
var order_pickup_time = Object.assign({}, this.orderPickupTimes[k]);
...という違いがあります。
それでも、問題の一部に過ぎません。あなたのコードの重要なアイデアは、それぞれの「アイテム」オブジェクト、つまり場所とピックアップ時間のリスト全体を保存することです。
しかし、これを行うには、すでに処理されたアイテム(構造が既に埋め込まれているアイテム)と新鮮なアイテムを区別する必要があります。それはそう、面倒だが、このようなもので動作するかもしれません:
var locations = [];
var item = this.items.find(it => it.item_id === line_item.menu_item_id);
if (item) {
locations = item.locations;
}
else {
item = {item_id: line_item.menu_item_id, header: line_item.menu_item_header, quantity: 0, locations: locations };
this.items.push(item);
}
...これは新しい場所やorderPickupTimeの作成のどちらかを選択する必要があるすべての時間を繰り返してきた - または既存のものを再利用します。
ここでは、このアプローチを示すthe demoです。
ここでも2つの主要な部分を変更します。
まず、注文アイテムをその場所と受注時間でグループ化する専用プライベート機能を作成します。このような何か:それはヘッダアレイからの場所とorderPickupTimesの順番をとるように
function _groupItemsByOrders(orders) {
return orders.reduce(function(groupedItems, order) {
var locationId = order.location_id;
var orderPickupTimeKey = order.order_pickup_time_short;
order.line_items.forEach(function(lineItem) {
if (!groupedItems.hasOwnProperty(lineItem.menu_item_id)) {
groupedItems[lineItem.menu_item_id] = {
header: lineItem.menu_item_header,
quantity: 0,
locations: {}
};
}
var groupedItem = groupedItems[lineItem.menu_item_id];
if (!groupedItem.locations.hasOwnProperty(locationId)) {
groupedItem.locations[locationId] = {};
}
var groupedItemLocation = groupedItem.locations[locationId];
if (!groupedItemLocation.hasOwnProperty(orderPickupTimeKey)) {
groupedItemLocation[orderPickupTimeKey] = 0;
}
groupedItemLocation[orderPickupTimeKey] += lineItem.quantity;
groupedItem.quantity += lineItem.quantity;
});
return groupedItems;
}, {});
}
第二に、私はその後、groupedDataにマップし、そのテンプレートを再配置のです。このような何か:
は
<tr v-for="(item, item_id) in groupedItems">
<td>{{item_id}}</td>
<td>{{item.header}}</td>
<td>{{item.quantity}}</td>
<template v-for="location in locations">
<td v-for="opt in orderPickupTimes">
{{item.locations[location.id][opt.short]}}
</td>
</template>
</tr>
今、あなたの 'マウント' フックは次のようになります。
mounted(){
axios.get('http://www.mocky.io/v2/59f8ceb52d00004d1dad41ec')
.then(response => {
this.locations = response.data.locations;
this.orderPickupTimes = response.data.order_pickup_times;
this.groupedItems = _groupItemsByOrders(response.data.orders);
});
}
はここthe demoです。
thx raina - ここ数日は走っていた。私はupvotedしかし、なぜ私が働いていないのかについてもっと多くの情報を与えることは可能です。私は詳細を反映するために賞金を加えました。もう一度。 – timpone
もう一度答えを更新し、代替アプローチのデモを提供しました。ところで、ロギングでコードの振る舞いを確認するには、console.log(JSON.stringify(object))などを使用します。そうでなければ、オブジェクトの_latest_状態が表示されます。ロギング。 – raina77ow