在javascript(jquery)中从数组对象中删除值

时间:2022-09-25 09:10:28

I am trying to delete objects from my array via javascript. Im building a simple mobile jquery application and want to delete items from an array. 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.

我试图通过javascript从我的数组中删除对象。我正在构建一个简单的移动jquery应用程序,并希望从数组中删除项目。我使用以下代码。我有一个包含复选框的列表。每个复选框都有一个属于列表项的值。因此,当检查多个复选框时,它应该从数组中删除所有这些对象(项)。

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.

烦人的事情如下:当我从数组切片对象时,在第二次迭代时恢复对象。所以它总是从数组中只删除一个对象。

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

Thanx回答和回应

Solution Thanx to depperm + indubitablee:

解决方案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();
}

3 个解决方案

#1


0  

There are a few things I would change, first in the for loop no need to call getObjects() each time just use objects. Then in the if simply check if the objects[i].title is the same as the inputValue.

我会改变一些事情,首先在for循环中,每次只使用对象时不需要调用getObjects()。然后在if中只检查对象[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();
}

#2


0  

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.

切片根本不会改变/操作原始数组,它只是根据您的选择创建另一个数组。

splice does alter the original array.

splice确实改变了原始数组。

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

#3


0  

The array method slice does not remove array elements. Use splice instead.

数组方法切片不会删除数组元素。改为使用拼接。

There are some problems with your code:

您的代码存在一些问题:

  1. You missed the keyword var sometimes.
  2. 您有时错过了关键字var。

  3. The variable i is equal to the length of the array in the first iteration. It should be one minus that.
  4. 变量i等于第一次迭代中数组的长度。它应该是一个减去那个。

  5. Instead of converting to JSON to compare the objects, you could have just compared the value of the title property.
  6. 您可以只比较title属性的值,而不是转换为JSON来比较对象。

#1


0  

There are a few things I would change, first in the for loop no need to call getObjects() each time just use objects. Then in the if simply check if the objects[i].title is the same as the inputValue.

我会改变一些事情,首先在for循环中,每次只使用对象时不需要调用getObjects()。然后在if中只检查对象[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();
}

#2


0  

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.

切片根本不会改变/操作原始数组,它只是根据您的选择创建另一个数组。

splice does alter the original array.

splice确实改变了原始数组。

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

#3


0  

The array method slice does not remove array elements. Use splice instead.

数组方法切片不会删除数组元素。改为使用拼接。

There are some problems with your code:

您的代码存在一些问题:

  1. You missed the keyword var sometimes.
  2. 您有时错过了关键字var。

  3. The variable i is equal to the length of the array in the first iteration. It should be one minus that.
  4. 变量i等于第一次迭代中数组的长度。它应该是一个减去那个。

  5. Instead of converting to JSON to compare the objects, you could have just compared the value of the title property.
  6. 您可以只比较title属性的值,而不是转换为JSON来比较对象。