[英]findIndex of lodash in es6
我始終依靠lodash和splice的findIndex來重新渲染更新后的數據,現在我正在拋棄lodash,因為我現在正在使用babel進行編譯,如何改進下面的代碼?
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);
這也將加快您的查找時間。
JS Array還有一個findIndex方法,該方法很有用:
onDoneUpdatedUserStatus(user) {
const index = this.users.findIndex(u => u.user_id === user.user_id);
if (index > -1) {
this.users.splice(index, 1, user);
}
}
這里是單線:
this.users = this.users.map(oldUser => oldUser.id === user.id ? user : oldUser);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.