AS3 一般从(对象)数组中删除元素
有没有一种方法可以从数组中普遍删除对象?
(也许不使用 array.filter 或创建新数组)
示例:
var arr:Array= new Array();
//create dummy objs
for (var i:uint=0; i < 10; i++){
var someObject:SomeClassObject = new SomeClassObject();
someObject.Name ="Amit"+ i;
someObject.Site="http://www.mysite.com/"+i;
//...many more props
arr.push(someObject);
}
//
removeElement("Amit4",arr);
removeElement("Amit8",arr);
//...so on so forth
目前我使用 array.splice() 来删除对象
for (var i:Number=0; i < arr.length; i++)
{
if (arr[i].Name == element)
{
arr.splice(i, 1);
}
}
我想以这样的方式编写removeElement,以便我可以将它用于不同的用途 对象类型。
目前,removeElement 变得依赖于实现..
假设如果我想从给定文件名的文件数组中删除一个文件..我必须这样做 通过更改条件再次写入“removeElement”。
另外,我可以改变标准吗? 示例:
arr= removeElement("Site","http://www.mysite.com/6",arr)
将从 arr 中删除“Site”属性等于“http://www.mysite.com/6”的对象 (使用上面的例子)
ie. removeElement(criteria:object,criteria_value(s):object,arr)
谢谢大家。
Is there a way to generically remove an object from an array?
(maybe not using array.filter or creating a new array)
Example:
var arr:Array= new Array();
//create dummy objs
for (var i:uint=0; i < 10; i++){
var someObject:SomeClassObject = new SomeClassObject();
someObject.Name ="Amit"+ i;
someObject.Site="http://www.mysite.com/"+i;
//...many more props
arr.push(someObject);
}
//
removeElement("Amit4",arr);
removeElement("Amit8",arr);
//...so on so forth
Currently im using array.splice() to remove object
for (var i:Number=0; i < arr.length; i++)
{
if (arr[i].Name == element)
{
arr.splice(i, 1);
}
}
I want to write removeElement in such a way that i can use it for different
types of objects.
currently removeElement becomes dependant on implmentation..
Suppose if i want to remove a file from array of files given file name..i wud have to
again write "removeElement" by changing criteria.
Also may be i can vary the criteria varing criteria?
example :
arr= removeElement("Site","http://www.mysite.com/6",arr)
will remove object from arr whose "Site" property is equal to "http://www.mysite.com/6"
(using above example)
ie. removeElement(criteria:object,criteria_value(s):object,arr)
Thanks All.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
Use
我认为最灵活的方法是
Array::filter
。由调用者通过函数回调来确定是否应从列表中过滤掉某个项目。现在,如果您想就地执行此操作,您可以编写一个像这样的简单函数:
这将返回列表,因为如果您想链接调用,它可能会很方便,但它作用于您传递的数组,而不是创建新的数组一。
另请注意,它向后循环。否则,拼接会给你带来虚假的结果。
你可以这样使用它:
这样你就不会受到平等(或不平等)的限制。调用者通过分别返回 true 或 false(以匹配
filter
)来确定是否应保留或删除该项目。因此,它非常类似于filter
,只不过它就地工作。如果需要,您还可以为回调保留相同的接口(传递项目的索引和对原始数组的引用)以使其更加连贯。I think the most flexible approach is the one followed by
Array::filter
. It's up to the caller to determine whether an item should be filtered out of the list or not, through a function callback.Now, if you want to do it in place, you could write a simple function like this:
This returns the list, as it could be convenient if you wanted to chain calls, but it acts on the array you passed instead of creating a new one.
Also note that it loops backwards. Otherwise, splice will get you bogus results.
You could use it like this:
This way you are not restricted to equality (or inequality). The caller determines if the item should be kept or removed, by returning true or false respectively (to match
filter
). So, it's pretty much likefilter
, except it works in-place. If you want, you could also keep the same interface for the callback (passing the index of the item and a reference to the original array) to make it more coherent.顺便说一句,您可以使用字符串作为数组的索引,然后您可以安全地使用“delete”关键字从数组的“中间”内部删除对象(在这种情况下实际上没有“中间”:) 。
例如:
缺点是你无法知道数组的长度(arr.length将返回0),但你当然可以自己跟踪它......
By the way, you can use strings as indices for an array, and then you can safely use the 'delete' keyword to delete an object from inside the "middle" (there's actually no "middle" in this situation :) of the array.
e.g.:
The disadvantage is you won't be able to know the length of the array (arr.length will return 0), but you can of-course track it yourself...
这是一个通用函数,它将执行您想要的操作:
Here is a generic function which will do what you want: