gpt4 book ai didi

javascript - es6中lodash的findIndex

转载 作者:行者123 更新时间:2023-11-29 19:04:44 25 4
gpt4 key购买 nike

我总是依赖于 lodash 的 findIndex 和 splice 来重新渲染我的更新数据,我希望在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com