0

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.

2
  • 1
    You are creating a new Date object every time. A new item will never equal an existing item. Commented Jun 17, 2014 at 13:10
  • @Brandon - lol very true - didn't even think of that Commented Jun 17, 2014 at 13:12

4 Answers 4

1

The two relevant pieces of code that are conflicting with each other are:

removeByValue(myArray, new Date(year, month, day));

if (arr[i].begin == val) {

You are creating a brand new object and then checking if it is the same object as an object that already existed. It can't be. It's brand new.

Instead of reference equality, if you can trust that arr[i].begin is a date, then you perhaps want a date comparison:

if (arr[i].begin.getTime() === val.getTime()) {

This takes both dates and converts them to an integer representation of the number of millisecond since 1970, then makes sure those integers match.

Sign up to request clarification or add additional context in comments.

Comments

0

You can compare dates via the internal timestamp:

if (arr[i].begin.getTime() == val.getTime()) {
    arr.splice(i, 1);
    break;
}

Comments

0

If you are trying to find and remove a value:

function removeByValue(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}

If you only care about the first value:

function removeByValue(arr, val) {
    if(arr[0] == val) {
         arr.splice(0, 1);
         break;
    }
}

2 Comments

I'm checking dates, and if the date is already in the array, I want it to be deleted. I don't want to delete just the first element. I can be any element in the array.
yah - then use the first one - that will search through the whole array to look for a match :)
0

You are comparing Two date type value. So you have to convert the value to Date using getTime() method.

function removeByValue(arr, val)
{
  var oldDate=val.getTime();
  var l=arr.length;
  for (var i = 0; i <l ; i++) {
    if(arr[i].begin.getTime() === oldDate) {
        arr.splice(i, 1);
         break;
       }
   } 
 }

1 Comment

It always helps to provide an explanation along with your code. Whether or not this solves the problem, it doesn't help the OP know why.