如何替换 immutableJs 中的集合
How to replace set in immutableJs
我有这段代码:
export function updated(list, el) {
const id = list.findIndex(item => item.id === el.id);
return id < 0 ? list.push(el) : list.set(id, el); // chiedere come
}
如何在没有 immutableJS 的情况下替换 list.set(id, el)
。
我试过
list.id = el
list = {
el: id,
}
list[id] = el;
都是错误。
你能给我一个建议吗?
更新
根据@code-apprentice 的要求
我不需要函数的不同行为,我需要从一个大项目中删除不可变的。
现在
list.id = el
list = {
el: id,
}
带有 airbnb 配置的 esLint 说这是 no-return-assign for this line
如果我尝试 list[id] = el
我会收到 no-return-assign for this line
和 no-param-reassign for this line
这将以不可变的方式return一个新对象。
return id < 0 ? list.concat(el) : {...list, id: el}
Caveats
As observed by Code-Apprentice if your object contains children that are reference-types themselves then you should spread then as well, cause the references inside will be maintained
我有这段代码:
export function updated(list, el) {
const id = list.findIndex(item => item.id === el.id);
return id < 0 ? list.push(el) : list.set(id, el); // chiedere come
}
如何在没有 immutableJS 的情况下替换 list.set(id, el)
。
我试过
list.id = el
list = {
el: id,
}
list[id] = el;
都是错误。
你能给我一个建议吗?
更新
根据@code-apprentice 的要求
我不需要函数的不同行为,我需要从一个大项目中删除不可变的。 现在
list.id = el
list = {
el: id,
}
带有 airbnb 配置的 esLint 说这是 no-return-assign for this line
如果我尝试 list[id] = el
我会收到 no-return-assign for this line
和 no-param-reassign for this line
这将以不可变的方式return一个新对象。
return id < 0 ? list.concat(el) : {...list, id: el}
Caveats
As observed by Code-Apprentice if your object contains children that are reference-types themselves then you should spread then as well, cause the references inside will be maintained