I'm using jQuery 1.9.1, and jQuery Mobile 1.3.1, for a small mobile app. At one point I have an array of objects, which has three element, one string, and two date objects. I'm adding dates to it, and I want to check the begin date, if a begin date is already inside the array, I want it to be removed.
var myArray = [];
function removeByValue(arr, val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].begin == val) {
arr.splice(i, 1);
break;
}
}
}
$("#cal").bind('change', function (event, date) {
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
removeByValue(myArray, new Date(year, month, day));
myArray.push({
"summary": "",
"begin": new Date(year, month, day),
"end": new Date(year, month, day)
});
});
The point is that this doesn't seem to be working. It keeps adding the dates to my array, if an already added date is in my array, it still adds it. Simply, my removeByValue function is now working. Any idea how to fix it? So, I basically want to check it the selected date is already in the array, if it is, I don't add it again to the array, if it is not, I then add it to the array.
Dateobject every time. A new item will never equal an existing item.