我有这些代码:
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
}
我该如何替换list.set(id, el)
没有immutableJS的代码。
我尝试过了
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 id < 0 ? list.concat(el) : {...list, id: el}
注意事项
正如代码学徒所观察到的,如果您的对象本身包含的是引用类型的子代,那么您也应该进行扩展,因为内部的引用将被保留
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句