【发布时间】:2018-01-11 17:24:28
【问题描述】:
我有这些代码:
var update=require('immutability-helper');
var state = {
step: 1,
fields: {
type: '',
name: '',
subtype: '',
team: '',
agreement: ''
}
};
function assignAttribute(attr, value) {
var temp = update(state, {
[attr]: {$set: value}
});
state = temp;
};
console.log(state); // -> { step: 1, fields: { type: '', name: '', subtype: '', team: '', agreement: '' } }
assignAttribute('step', 3);
console.log(state); // -> { step: 3, fields: { type: '', name: '', subtype: '', team: '', agreement: '' } }
assignAttribute('fields.type','superType');
console.log(state); // -> { step: 3, fields: { type: '', name: '', subtype: '', team: '', agreement: '' }, 'fields.type': 'superType' }
我想要的是最后一行如下:
{ step: '3', fields: { type: 'superType', name: '', subtype: '', team: '', agreement: '' } }
主要问题似乎是在assignAttribute函数中[attr]不再用作点符号 我该怎么办?
【问题讨论】:
标签: javascript json reactjs nested immutability