简体   繁体   English

从JavaScript中的数组对象中删除值(jquery)

[英]Delete value from array object in javascript (jquery)

I am trying to delete objects from my array via javascript. 我试图通过javascript从数组中删除对象。 Im building a simple mobile jquery application and want to delete items from an array. 我正在构建一个简单的移动jquery应用程序,并希望从数组中删除项目。 I use the following code. 我使用以下代码。 I have a list with checkboxes in it. 我有一个带有复选框的列表。 Every checkbox has a value that belongs to the list item. 每个复选框都有一个属于列表项的值。 So when multiple checkboxes are checked.. it should delete all these objects (items) from the array. 因此,当选中多个复选框时,它应该从数组中删除所有这些对象(项目)。

function deleteFunction()
    {
        objects = getObjects();
        $("input:checked").each(function()
        {
            var inputValue = $(this).val();
            for(i = getObjects().length; i >=0; i--)
            {

                if('{"title":"'+ inputValue + '"}' == JSON.stringify(objects[i]))
                {
                    objects.splice(i, 1);
                    return true;
                }
            }

        });
        alert(JSON.stringify(objects));
        window.location.reload();
    }

The annoying thing is as follows: When i slice the object from the array, the object is restored on the second iteration. 令人讨厌的事情如下:当我从数组中切片对象时,对象在第二次迭代中恢复。 So it always removes only 1 object from the array. 因此,它总是只从数组中删除1个对象。

To test my output i used the following code within the if statement: 为了测试我的输出,我在if语句中使用了以下代码:

alert(i);
alert(JSON.stringify(objects[i]));  
objects.splice(i, 1);
alert(i);
alert(JSON.stringify(objects)); 
return true;    

The output is as follows 输出如下

1
{"title":"hi2"}
1
[{"title":"hi1"}, {"title":"hi3"}]
2
{"title":"hi3"}
2
[{"title":"hi1"}, {"title":"hi2"}]

so i slice hi2, but has returned 所以我切片hi2,但已经返回

Thanx for answer and respond 感谢回答和回应

Solution Thanx to depperm + indubitablee: 解决方案,以消除敌意+坚不可摧:

function deleteFunction()
{
    var objects = getObjects();
    $("input:checked").each(function()
    {
        var inputValue = $(this).val();
        for(i = objects.length -1; i >=0; i--)
        {           
            if(objects[i].title == inputValue)
            {                               
                objects.splice(i, 1);           
            }
        }

    });
    localStorage.setItem("objects", JSON.stringify(objects));
    window.location.reload();
}

There are a few things I would change, first in the for loop no need to call getObjects() each time just use objects . 我会改变一些事情,首先在for循环中,每次只使用objects时都不需要调用getObjects() Then in the if simply check if the objects[i].title is the same as the inputValue . 然后,在if简单地检查objects[i].title是相同inputValue

function deleteFunction()
{
    objects = getObjects();
    $("input:checked").each(function()
    {
        var inputValue = $(this).val();
        for(i = objects .length; i >=0; i--)
        {
            if(objects[i].title==inputValue)
            {
                objects.splice(i, 1);                     
            }
        }

    });
    alert(JSON.stringify(objects));
    window.location.reload();
}

use .splice() instead of .slice() . 使用.splice()而不是.slice()

slice does NOT alter/manipulate the original array at all, it just creates another array based on your selection. slice根本不会更改/操纵原始数组,它只是根据您的选择创建另一个数组。

splice does alter the original array. splice确实会更改原始数组。

reference: http://www.devcurry.com/2010/12/slice-and-splice-in-javascript.html 参考: http : //www.devcurry.com/2010/12/slice-and-splice-in-javascript.html

The array method slice does not remove array elements. 数组方法slice不会删除数组元素。 Use splice instead. 请改用splice

There are some problems with your code: 您的代码存在一些问题:

  1. You missed the keyword var sometimes. 您有时错过了关键字var
  2. The variable i is equal to the length of the array in the first iteration. 变量i等于第一次迭代中数组的长度。 It should be one minus that. 应该减去一。
  3. Instead of converting to JSON to compare the objects, you could have just compared the value of the title property. 您可以只比较title属性的值,而不用转换为JSON来比较对象。

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

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