我想使用带有变量作为键的 Object.assign



我有一个操作"set-style"发送到一个化简器,在那里我更改了对象数组中按键指定的对象的样式对象,命名层。

困难在于 - "filterName"用于接收需要更改的属性名称。如果我使用另一个属性名称,它的行为确实正确,但"filterName"变量不能用作键。

export const set_stl = (val,filterName,chosenLayerId,chosenLayerIdx=0,layers) => {
const specLayer = layers[chosenLayerIdx];
const assignedSpecLayer = Object.assign({}, specLayer, {
    stl: {
        ...specLayer.stl,
        filterName: val
    }
});
const newLayers = layers;
    newLayers[chosenLayerIdx] = assignedSpecLayer;
return dispatch => {
    dispatch({
        type: SET_STL,
        payload: newLayers,
    })
}

};

您需要使用

[] 来使用filterName的值。

stl: {
    ...specLayer.stl,
    [filterName]: val
}

最新更新