简体   繁体   English

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

[英]How to delete a specific element from an array of JavaScript objects?

I am receiving a JSON output from a php file, with a number of objects like so: 我从php文件接收到JSON输出,其中包含许多对象,如下所示:

[
  { "home_content" : "Nam feugiat sem diam, ut fermentum orci hendrerit sit amet.",
    "home_id" : 2,
    "home_img" : "tech.png",
    "home_title" : "Latest Technologies Development"
  },
  { "home_content" : "לורם לורם",
    "home_id" : 239,
    "home_img" : "http://placehold.it/400",
    "home_title" : "שוק פירות וירקות"
  },
  { "home_content" : "New Item Content",
    "home_id" : 259,
    "home_img" : "http://www.placehold.it/100",
    "home_title" : "New Home Item"
  }
]

In my App I want to delete a certain object, is there a way to recieve its position say by home_id ? 在我的应用程序中,我想删除某个对象,有没有办法通过home_id接收它的位置? or anything that will let me differentiate a certain object from that list 或能让我从列表中区分某个对象的任何东西

What you have there is an array of objects, so you can just loop through the array until you find the one with the desired home_id : 您拥有的是一个对象数组,因此您可以遍历该数组,直到找到具有所需home_id

var index;
for (index = 0; index < array.length; ++index) {
    if (array[index].home_id === 2) {
        array.splice(index, 1); // Removes this entry
        break;                  // Exits the loop
    }
}

What you have is an array (obtained from parsing JSON), so you'll have to find the correct index, and splice it: 您拥有的是一个数组(通过解析JSON获得),因此您必须找到正确的索引并进行拼接:

function delObjWithHomeId(arr, id) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i].home_id === id) {
            return arr.splice(i,1);
        }
    }
}

What you've posted there is an array of objects. 您在此处发布的内容包含一系列对象。 That's an important distinction -- though in javascript, and array is a type of object. 这是一个重要的区别-尽管在javascript中,而array 一种对象。

An object, as you see, can have alphanumberic property names (indexes, if you will) that correspond to a data element. 如您所见,对象可以具有对应于数据元素的字母数字属性名称(如果可以的话,可以使用索引)。 An array, by contrast, is numerically indexed, starting at 0. It is a lot like an object, but the property names are all numbers, and all in sequential order. 相比之下,数组是从0开始的数字索引。它很像对象,但是属性名称都是数字,并且都是按顺序排列的。

So, imagine your data like this: 因此, 假设您的数据是这样的:

{
0:{ 
home_id: 2
home_title: Latest Technologies Development
home_img: tech.png
home_content: Nam feugiat sem diam, ut fermentum orci hendrerit sit amet.
},
1:{ 
home_id: 239
home_title: שוק פירות וירקות
home_img: http://placehold.it/400
home_content: לורם לורם
},
2:{ 
home_id: 259
home_title: New Home Item
home_img: http://www.placehold.it/100
home_content: New Item Content
}
}

You can use a number of methods to manipulate an array, like push() , pop() , shift() . 您可以使用多种方法来操作数组,例如push()pop()shift() To access a specific one, you usually wind up looping it (unless you already know the correct index ). 要访问一个特定的索引 ,通常会结束循环(除非您已经知道正确的索引 )。

There are several methods to looping an array, while being one of the many possible approaches: 有多种循环数组的方法, while也是许多可能的方法之一:

var i = 0;
while(var oneItem = myArray[i++]){
    if (oneItem.home_id == theItemIdIAmLookingFor) {
        myArray.splice(i, 1);
        break;
    }
}

Documentation 文献资料

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

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