簡體   English   中英

如何防止 javascript object 被轉換為長度為 1 的對象數組?

[英]How do you prevent a javascript object from being converted into a length 1 array of objects?

我正在處理我的第一個單獨的 ReactJS/Redux 項目,事情進展順利,直到我在 Redux 商店中使用 object 時,它總是應該是一個單一的 ZA8CFDE6331BD4B66AC96F8911C。 當我將 object 從存儲的一部分(源鍵的一個元素)復制到另一部分(selectedItems 鍵)時,object 被存儲為長度為 1 的數組,這不是我傳入的數據(它只是一個對象)。 我可以忍受這一點,只需將該存儲變量作為數組讀出,然后只使用元素 0,除非當我在 reducer 中調用另一個方法來替換存儲中的該變量時,該方法將新數據存儲為單個 object。 我的偏好是讓所有東西都存儲一個 object 但我不知道該怎么做,無論如何:這是一些減速器代碼:

const initialState = {
    sources: [
        {
            id: 1,
            mfg: 'GE',
            system: 'Smart bed',
            model: '01',
            name: 'GE smart bed'
        },
        {
            id: 2,
            mfg: 'IBM',
            system: 'Patient monitor',
            model: '03',
            name: 'IBM patient monitor'
        }
    ],
    error: null,
    loading: false,
    purchased: false,
    selectedItem: {}
};

// This is called when a user selects one of sources in the UI
// the Id of the selected sources object is passed in as action.id
// This method creates an array in state.selectedItem 
const alertSourceSelect = ( state, action ) => {
    let selectedItem = state.sources.filter(function (item) {
        return item.id === action.id;
    });

    if (!selectedItem) selectedItem = {};
    return {...state, selectedItem: selectedItem};
};

// When someone edits the selected source, this takes the field name and new value to 
// replace in the selected source object and does so. Those values are stored in 
// action.field and action.value . However, when the selected source object is updated
// it is updated as a single object and not as an array.
const selectedSourceEdit = ( state, action ) => {
    return {
        ...state,
        selectedItem: updateObject(state.selectedItem[0], { [action.field] : action.value })
    };
};

const reducer = (state = initialState, action) =>  {
        switch (action.type) {
        case actionTypes.ALERT_SOURCE_SELECT: return alertSourceSelect( state, action );
        case actionTypes.ALERT_SELECTED_SOURCE_EDIT: return selectedSourceEdit( state, action );
        default: return state;
    }

這是 updateObject 方法(對不起,我遺漏了它):

export const updateObject = (oldObject, updatedProperties) => {
    return {
        ...oldObject,
        ...updatedProperties
    }
};

問題: updateObject返回 object 而不是數組,並且您將selectedItem維護為數組而不是 object

export const updateObject = (oldObject, updatedProperties) => {
    return {
        ...oldObject,
        ...updatedProperties
    }
};

解決方案:

updateObject返回數組:

export const updateObject = (oldObject, updatedProperties) => {
    return [{
        ...oldObject,
        ...updatedProperties
    }]
};

或制作返回的 object 數組

const selectedSourceEdit = ( state, action ) => {
    return {
        ...state,
        selectedItem: [updateObject(state.selectedItem[0], { [action.field] : action.value })]
    };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM