在Javascript中从关联数组中删除项目

时间:2022-02-28 18:11:12

I have an Array of objects, example

我有一个对象数组,例如

Item[0] ="{ id1: 1}";
Item[1] ="{ id2: 2}"; 
Item[2] ="{ id3: 3}"; 

I have to delete an item by knowing an specific id. For example, if i get id2, i have to delete Item[1].

我必须通过了解特定的ID来删除项目。例如,如果我得到id2,我必须删除Item [1]。

I tried to solve it, but it deletes the last item

我试图解决它,但它删除了最后一项

for (var i = 0; i < items.length; i++) {
            var _item = items[i];
            var funcId = getValueKey(_item);

        if(funcId == _item)
        {
            delete items[i];
        }      
        };

The getValueKey func

getValueKey函数

getValueKey: function(obj){
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                return key;

            }
        }

2 个解决方案

#1


1  

I solved it, thanks to me..

我解决了,谢谢我..

for (var i = 0; i < items.length; i++) {
            var _item = items[i];
            var funcId = getValueKey(_item);
            if(funcId ==id) {
                delete items[i];
            }

#2


1  

Your answer may work, but it seems like much more code than is required.

您的答案可能有效,但似乎代码要多得多。

function removeMemberByValue(arr, value) {

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    if (arr[i] && arr[i].hasOwnProperty(value));

      //Do 1 of the following, not both:

      // to remove member i (i.e. there will no longer be a member i)
      delete(arr[i]);

      // Remove member i and shift later members -1
      arr.splice(i, 1);

      return arr;  // return is optional
  }
}

In the above, the first option will result in removal of the member but all other members will keep the same index, e.g.

在上文中,第一个选项将导致成员被删除,但所有其他成员将保持相同的索引,例如,

var arr = [{id1: 1}, {id2: 2}, {id3: 3}];
removeMemberByValue(arr, value); // [not defined, {id2: 2}, {id3: 3}]

where not defined means "does not exist".

未定义的意思是“不存在”。

The second option will move later members 1 lower in index, so:

第二个选项将后面的成员1移动到索引较低的位置,因此:

removeMemberByValue(arr, value); // [{id2: 2}, {id3: 3}]

Choose whichever suits.

选择适合的任何一种。

#1


1  

I solved it, thanks to me..

我解决了,谢谢我..

for (var i = 0; i < items.length; i++) {
            var _item = items[i];
            var funcId = getValueKey(_item);
            if(funcId ==id) {
                delete items[i];
            }

#2


1  

Your answer may work, but it seems like much more code than is required.

您的答案可能有效,但似乎代码要多得多。

function removeMemberByValue(arr, value) {

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    if (arr[i] && arr[i].hasOwnProperty(value));

      //Do 1 of the following, not both:

      // to remove member i (i.e. there will no longer be a member i)
      delete(arr[i]);

      // Remove member i and shift later members -1
      arr.splice(i, 1);

      return arr;  // return is optional
  }
}

In the above, the first option will result in removal of the member but all other members will keep the same index, e.g.

在上文中,第一个选项将导致成员被删除,但所有其他成员将保持相同的索引,例如,

var arr = [{id1: 1}, {id2: 2}, {id3: 3}];
removeMemberByValue(arr, value); // [not defined, {id2: 2}, {id3: 3}]

where not defined means "does not exist".

未定义的意思是“不存在”。

The second option will move later members 1 lower in index, so:

第二个选项将后面的成员1移动到索引较低的位置,因此:

removeMemberByValue(arr, value); // [{id2: 2}, {id3: 3}]

Choose whichever suits.

选择适合的任何一种。