i am trying to merge 2 array of Date objects uniqely, but don't get it the decent way. Here is my noob solution:
$old = [new Date('2012-08-01'), new Date('2012-08-02'), new Date('2012-08-03')];
$new = [new Date('2012-08-01'), new Date('2012-08-06')];
$old2 = $.map($old, function(el, idx) {
for (var i in $new)
{
if ($new[i].getTime() == el.getTime()) return null;
}
return el;
});
$new2 = $.map($new, function(el, idx) {
for (var i in $old)
{
if ($old[i].getTime() == el.getTime()) return null;
}
return el;
});
$final = $.merge($old2, $new2);
This works, but it seems to be kinda nooby and i guess inperformat too? Another try i made was this:
for (var i in $new)
{
var found = false;
for (var j in $old)
{
if ($old[j].getTime() == $new[i].getTime())
{
found = true;
$old = $old.splice(j, 1);
}
}
if (found)
{
$new = $new.splice(i, 1);
}
}
$final = $.merge($old, $new);
But this does not work at all. How to do it correctly and performant?