Created
October 30, 2016 13:57
-
-
Save melnik88/a2076e193a40d139d8270a0552d2c82d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let users = [ | |
{id: 1, name: 'user1', status: 'online'}, | |
{id: 2, name: 'user2', status: 'offline'}, | |
{id: 3, name: 'user3', status: 'invisible'} | |
] | |
let updatedUser = {id: 4, name: 'user2', status: 'lalala'} | |
let index = -1; | |
let oldItems = users; | |
let newItems = []; | |
for (var i = 0; i < oldItems.length; i++) { | |
if (oldItems[i].id === updatedUser.id) { | |
index = i; | |
break; | |
} | |
} | |
if (index >= 0) { | |
newItems = [...oldItems.slice(0,index), updatedUser,...oldItems.slice(index + 1, oldItems.length)]; | |
} | |
console.log(newItems); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment