次のオブジェクトの配列が与えられた場合、入れ子にされた各 "locationData"配列オブジェクトのqtyをどのように更新すればよいでしょうか?結局のところ、私はオブジェクト内のすべてではなく、すべてのオブジェクト内のすべてを意味します。基本オブジェクトはItemといい、locationDataはアイテムが入っている場所です。Reduxで複数のネストされたオブジェクトを更新する方法
私が行っているのは、場所のあるすべてのアイテムをユーザーに表示することで、別のボタンをクリックして割り当てられていない数量選択した分布ですべてのオブジェクトを更新する場所に移動します。
- Reactコンポーネントのデータをループして、新しいlocationData配列を持つ各アイテムのディスパッチを行い、古いものを置き換えますか?
- アクション内のアイテムをループして、新しいlocationDataで全く新しいアイテムの配列でリダンダントを呼び出すことはできますか?
私はImmutableJsの有無にかかわらず、これを行うことができますが、これを実行する最良の方法はわかりません。
[
{
"isSelected":false,
"itemid":"2557",
"name":"Accessories : Awesome Cable",
"quantity":6000,"quantityByCase":600,
"locationData":[
{"id":"5","name":"QA Location","enabled":true,"percentage":"200","shipcarrier":"","shipmethod":"","qty":0},
{"id":"7","name":"Single Bin Location","enabled":true,"percentage":"50","shipcarrier":"","shipmethod":"","qty":0},
{"id":"1","name":"Warehouse - East Coast","enabled":true,"percentage":"","shipcarrier":"1","shipmethod":"92","qty":0}
],
"unAllocatedQuantityByCase":600
},
{
"isSelected":false,
"itemid":"40",
"name":"Accessories : Crusher Game Pad",
"quantity":50,"quantityByCase":50,
"locationData":[
{"id":"5","name":"QA Location","enabled":true,"percentage":"200","shipcarrier":"","shipmethod":"","qty":0},
{"id":"7","name":"Single Bin Location","enabled":true,"percentage":"50","shipcarrier":"","shipmethod":"","qty":0},
{"id":"1","name":"Warehouse - East Coast","enabled":true,"percentage":"","shipcarrier":"1","shipmethod":"92","qty":0}
],
"unAllocatedQuantityByCase":50}]
現在、私は私の減速EVEN_DISTRIBでの計算をしていますが、これは右に感じることはありません。私がやっていることは、状態全体を取り出し、それをitems変数にマッピングすることです。 items.isSelectedは現時点ではテストのためにtrueにハードコードされています。次に、各アイテムに対して 'unAllocatedQuantityByCase'の均等分布を行い、最後にこのレデューサーのすべてのアイテムと新しい状態を返します。後で加重されたアイテムの分布もありますが、さらに進める前に均等な分布を得たいと思っていました。
export default function (state = [], action) {
switch (action.type) {
case types.PO_RECEIVED:
return state.concat(action.payload);
case types.EVEN_DISTRIB: {
let items = state.map(item => {
item.isSelected = true;
if (item.isSelected) {
let quantity = item.unAllocatedQuantityByCase;
let updateLocations = item.locationData.filter(location => location.enabled);
let remainder = quantity % updateLocations.length;
let divideqty = (quantity - remainder)/updateLocations.length;
let loc = updateLocations.map(location => {
var tempQty = divideqty;
if (remainder >= 1) {
tempQty++;
remainder--;
} else if (remainder > 0) {
tempQty += remainder;
remainder = 0;
}
location.qty = tempQty;
return location
});
item.locationData = loc;
}
return item;
});
return items;
}
default:
return state;
}}
還元剤でこれらの計算を行うべきですか?コンポーネント内でそれらを実行し、それをペイロードとして呼び出す必要がありますか?またはこれを行う良い方法があります...データ構造を正規化し、すべての場所を更新するだけです。私は私の質問は、これらのタイプの計算がReact/Reduxアプリケーションでどこで行われるのかということですね。