[英]How to delete an object from array by reference (javascript)?
Simple question, but I cannot find a solution. 简单的问题,但我找不到解决方案。
I have an array of objects. 我有一个对象数组。 I also have a reference to an object from this array.
我也从该数组引用了一个对象。
I want to delete the object from the array. 我想从数组中删除对象。
How to do it in Javascript (without comparing object properties)? 如何在Javascript中进行操作(不比较对象属性)?
PS It is easy to do it in C# (using List collection) PS在C#中很容易做到(使用列表集合)
List<SomeObject> list = ........ ;
SomeObject element = ......... ;
list.Remove(element);
You can use indexOf to get the index of the object and splice to remove it from the array: 您可以使用indexOf获取对象的索引,并进行拼接以将其从数组中删除:
var arr = [ { name: 0}, { name : 1 } , {name : 2 } ];
var myObj = arr[1];
arr.splice(arr.indexOf(myObj),1);
console.log(arr);
There is no way to do this with arrays directly. 无法直接对数组执行此操作。 You will have to find or roll your own implementation of an collection which supports similar operation.
您将必须查找或滚动自己的支持类似操作的集合的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.