简体   繁体   中英

findIndex of lodash in es6

I always depends on findIndex of lodash and splice to rerender my updated data, I'm looking to ditch lodash as I'm compiling with babel now, what can be done to improve below code?

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);
    }
}

You can store your users in a Map from user ids to users instead of an array. Then you just need to write

this.users.delete(user.user_id);

This will also speed up your lookup time.

JS Array also has a findIndex method which is useful:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM