简体   繁体   English

如何从对象数组中删除元素?

[英]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. 我将jQuery 1.9.1和jQuery Mobile 1.3.1用于小型移动应用程序。 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. 简而言之,我的removeByValue函数现在可以正常工作了。 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: 如果您可以相信arr[i].begin是一个日期,那么可以使用日期比较:

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. 这将同时使用两个日期,并将它们转换为以1970年以来的毫秒数表示的整数,然后确保这些整数匹配。

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. 因此,您必须使用getTime()方法将值转换为Date。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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