我总是依赖 lodash 和 splice 的 findIndex 来重新渲染更新后的数据,我现在正在使用 babel 进行编译,因此希望放弃 lodash,可以做些什么来改进下面的代码?
onDoneUpdatedUserStatus(user) {
//this.users means existing array of object of existing users
const index = findIndex(this.users, { user_id: user.user_id });
if (index > -1) {
this.users.splice(index, 1, user);
}
}
最佳答案
您可以将用户存储在从用户 ID 到用户的 Map
中,而不是数组。然后你只需要写
this.users.delete(user.user_id);
这也会加快您的查找时间。
关于javascript - es6中lodash的findIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828223/