Today I came across a SO question to replace the corresponding object inside an array of objects.
To do this, they find the index of the corresponding object inside the array of objects using lodash .
var users = [{user: "Kamal"}, {user: "Vivek"}, {user: "Guna"}] var idx = _.findIndex(users, {user: "Vivek"});
Now they used splice () to replace as follows:
users.splice(idx, 1, {user: "Gowtham"})
but why not
users[idx] = {user: "Gowtham"};
Now my question is: is there any reason rather than using or splice () ?
Because it is so simple to use array[index] = 'something'; . Is not it?
source share