简体   繁体   中英

How to remove element from an array of objects?

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.

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.

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;
    }
}

You can compare dates via the internal timestamp:

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

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;
       }
   } 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM