简体   繁体   English

如何防止 javascript object 被转换为长度为 1 的对象数组?

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

I'm working on my first solo ReactJS/Redux project and things were going well until I got to a point where I'm using an object in the Redux store that is always supposed to be a single object.我正在处理我的第一个单独的 ReactJS/Redux 项目,事情进展顺利,直到我在 Redux 商店中使用 object 时,它总是应该是一个单一的 ZA8CFDE6331BD4B66AC96F8911C。 When I copy the object from one part of the store (one element of the sources key) to another (the selectedItems key) that object is being stored as an array of length 1, which isn't the data I'm passing in (it's just a single object).当我将 object 从存储的一部分(源键的一个元素)复制到另一部分(selectedItems 键)时,object 被存储为长度为 1 的数组,这不是我传入的数据(它只是一个对象)。 I could live with this and just read out of that store variable as an array and just use element 0 except that when I call another method in the reducer to replace that variable in the store, that method stores the new data as a single object.我可以忍受这一点,只需将该存储变量作为数组读出,然后只使用元素 0,除非当我在 reducer 中调用另一个方法来替换存储中的该变量时,该方法将新数据存储为单个 object。 My preference would be to have everything store a single object but I can't figure out how to do that, Anyway: here's some of the reducer code:我的偏好是让所有东西都存储一个 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;
    }

Here is the updateObject method (sorry I left it out):这是 updateObject 方法(对不起,我遗漏了它):

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

Issue: updateObject is returning object and not array,and you are maintaining selectedItem as an array not object问题: updateObject返回 object 而不是数组,并且您将selectedItem维护为数组而不是 object

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

Solution:解决方案:

Either return array from updateObject :updateObject返回数组:

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

OR make array of returned object或制作返回的 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