[英]Ember : How to update a single field in an array of JSON objects in a controller?
[英]How to update one field in Array of Json Objects?
我在做reactjs,所以我需要立即更新一個字段來觸發狀態更改。
我有這個有效載荷(只顯示1,但它是許多的數組)
[
{
id: 1,
name: "Fridge2",
selected: true,
sharingId: 'ae9b9566-3b5c-4772-a0a1-07ed8b354b8f',
sharingWith: ["jim@hotmail.com", "jim2@hotmail.com"],
storageItems: [
{
id: 'ae9b9564-3b5c-2711-a421-07ed8b354b8f',
name: 'Chicken Breats',
qty: 10,
expiresOn: '3',
category: 'Meat',
categoryId: 'BDEC0494-B16E-411B-8E32-A64A00E943F8',
unitType: 'Pieces',
unitTypeId: '63CDB076-C20D-4DC5-A181-A64A00E94409'
},
{
id: 'ae9b9566-3b5c-2711-a4a1-07ed8b354b8f',
name: 'Chicken Breats2',
qty: 10,
expiresOn: '0',
category: 'Meat',
categoryId: 'BDEC0494-B16E-411B-8E32-A64A00E943F8',
unitType: 'Pieces',
unitTypeId: '63CDB076-C20D-4DC5-A181-A64A00E94409'
},
{
id: 'ae9b9566-3b5c-2712-a0a1-07ed8b354b8f',
name: 'Chicken Breats3',
qty: 10,
expiresOn: '4',
category: 'Meat',
categoryId: 'BDEC0494-B16E-411B-8E32-A64A00E943F8',
unitType: 'Pieces',
unitTypeId: '63CDB076-C20D-4DC5-A181-A64A00E94409'
}
]
}
]
我想找到與ID'ae9b9564-3b5c-2711-a421-07ed8b354b8f'(數組中的第一個)匹配的storageItem
然后,我想將其取出來更新字段(例如,數量)並重新插入並發生狀態更改。
這是我非常糟糕的第一次嘗試。 這是行不通的
case actions.STORAGE_ITEM_USED: {
var foundItem = state.selectedStorage.storageItems.filter(i => i.id == action.payload);
var newQty = foundItem[0].qty - 1;
foundItem[0].qty = newQty;
var nonChangedStorageItem = state.selectedStorage.storageItems.filter(i => i.id != action.payload);
var allItems = nonChangedStorageItem.concat(foundItem);
state.selectedStorage.storageItems = allItems;
return {
selectedStorage: state.selectedStorage,
}
}
編輯
我現在有這個,但我看到了一個新的可能答案,我將結帳
var newSelectedStorage = Object.assign({} , state.selectedStorage);
var foundItem = newSelectedStorage.storageItems.filter(x => x.id == action.payload);
foundItem[0].qty = foundItem[0].qty - 1;
var nonChangedItems = newSelectedStorage.storageItems.filter(x => x.id != action.payload);
newSelectedStorage.storageItems = nonChangedItems.concat(foundItem);
webpack.config.js
module.exports = {
devtool: 'inline-source-map',
entry: "./app/index.js",
output: {
path: __dirname + '/dist',
filename: "bundle.js"
},
devServer: {
contentBase: "./app",
inline: true,
port: 3333
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
loader: 'url-loader'
}
]
},
externals: {
jquery: 'jQuery'
},
}
從外觀qty
,您正在嘗試減少state.selectedStorage.storageItems
任何匹配對象的qty
屬性。
由於Redux需要一個全新的對象,因此我們可以使用ES6的對象散布運算符返回已經填充了大多數值的新對象。
case actions.STORAGE_ITEM_USED:
return {
...state,
selectedStorage: state.selectedStorage.storageItems.map(i => {
if (i.id != action.payload) return i;
return {
...i,
qty: i.qty - 1
}
})
}
我無法測試這種方法是否可行,但是我們的想法是,我們將返回一個新對象,復制現有狀態對象,然后用一個新數組覆蓋selectedStorage
,其中僅減少其id
匹配action.payload
的qty
屬性的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.