Skip to content

Instantly share code, notes, and snippets.

@edtoken
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save edtoken/a66b8a7601ba51561777 to your computer and use it in GitHub Desktop.

Select an option

Save edtoken/a66b8a7601ba51561777 to your computer and use it in GitHub Desktop.
(function ($) {
/**
* arrayUpdateSlice.
* Join arrays, update items with new data.
* add new items of updates array
* @param {array=} items basic array
* @param {array=} updates and new items array
* @returns {Array}
*/
var arrayUpdateSlice = function (items, updates) {
var array = items.concat(updates.slice(items.length));
array.map(function(item, i){
if (i < items.length) {
if (updates[i]) {
$.extend(item, updates[i]);
} else {
item['deleted'] = true;
}
}
});
return array;
};
var items = [
{id: '1', name: "a"},
{id: '2', name: "b"},
{id: '3', name: "c"},
{id: '4', name: "d"},
{id: '5', name: "e"},
{id: '6', name: "f"},
{id: '7', name: "g"},
{id: '8', name: "h"}
];
var updates = [
{name: "a->a"},
{name: "b"},
{name: "c"},
{name: "d"},
{name: "e->e"},
{name: "f"},
{name: "g"},
{name: "h->h"},
{name: "X"},
{name: "Z"}
];
var updateItems = arrayUpdateSlice(items, updates);
console.log('updateItems', updateItems.length, updateItems);
for (var i = 0; i < updateItems.length; i++) {
console.log('item', updateItems[i]);
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment