{
'1': {
id: 1,
parent_id: 0,
elementType: 'view',
style: {color: 'green'},
selected: false,
childIds: [2]
},
'2': {
id: 2,
parent_id: 1,
elementType: 'text',
style: {color: 'green'},
selected: true,
childIds: []
}
}
这是一个 reducer ,我很乐意能够做到这一点:
obj.map(([key, value])=> {
if (value.id === 1) {
return {...value, selected:false};
} else {
return value;
}
});
我想也许使用 this但它可能无法很好地与 redux 映射配合使用。
更新:
按照此处的建议,我尝试构建一个新的 map 功能。这个版本是我附带的:
const map = (object, callback) => {
return Object.keys(object).reduce(function(output, key) {
output[key] = callback.call(this, object[key]);
return output;
}, {});
}
我尝试像这样使用它(示例):
let newState = map(oldState, element => {
if (element.id === 1) {
return {...element, selected:false};
} else {
return {...element};
}
});
同时测试正在通过。
最佳答案
尝试使用Object.keys(),它可以让你遍历对象键,这样你就可以访问特定的值。
关于javascript - Redux ES6(BabelJS): is there a map function that can iterate this structure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275197/