Created
March 8, 2015 12:48
-
-
Save eyy/2a09ef6228edcc2c0100 to your computer and use it in GitHub Desktop.
update array
This file contains hidden or 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
function updateArray (arr, newArr) { | |
if (!arr.index) | |
arr.index = arr.reduce(function(seed, item) { | |
seed[item.id] = item | |
return seed | |
}, {}) | |
newArr.forEach(function(item) { | |
if (arr.index[item.id]) | |
return _.assign(arr.index[item.id], item) | |
arr.index[item.id] = item | |
arr.push(item) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment