This is happening because of the way JavaScript passes parameters. ui.content
is a copy of a reference to an array. You cannot modify that reference and expect the results to be visible outside of the function. This is better explained here.
For a simplified example, check this out:
function modify(item) {
item.arr = [];
}
var obj = {
text: 'hi',
arr: [1,2,3,4]
};
modify(obj);
console.log(obj);
Fiddle: http://jsfiddle.net/9swwA/1/
The log statement will show that obj.arr
is still [1, 2, 3, 4]
. You essentially have the same problem.
So to get around this you can use splice
to remove the items from the array. Unfortunately you're going to have to add a bit more code:
i = ui.content.length - 1
while i >= 0
currentItem = ui.content[i]
index = $.inArray(currentItem, current)
ui.content.splice i, 1 if index is -1
i--