AS3 一般从(对象)数组中删除元素

发布于 2024-09-25 12:25:21 字数 1144 浏览 2 评论 0原文

有没有一种方法可以从数组中普遍删除对象?
(也许不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱她像谁 2024-10-02 12:25:21

使用

if(array.indexOf(obj) != -1)
array.splice(array.indexOf(obj),1); 

Use

if(array.indexOf(obj) != -1)
array.splice(array.indexOf(obj),1); 
錯遇了你 2024-10-02 12:25:21

我认为最灵活的方法是Array::filter。由调用者通过函数回调来确定是否应从列表中过滤掉某个项目。

现在,如果您想就地执行此操作,您可以编写一个像这样的简单函数:

function remove(list:Array,callback:Function):Array {
    for(var i:int = list.length - 1; i >= 0; i--) {
        if(!callback(list[i])) {
            list.splice(i,1);
        }
    }
    return list;
}

这将返回列表,因为如果您想链接调用,它可能会很方便,但它作用于您传递的数组,而不是创建新的数组一。

另请注意,它向后循环。否则,拼接会给你带来虚假的结果。

你可以这样使用它:

var arr:Array = [1,2,9,10,455];
trace(arr);

function removeCallback(item:Number):Boolean {
    return item < 10;
}
remove(arr,removeCallback);
trace(arr);

这样你就不会受到平等(或不平等)的限制。调用者通过分别返回 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:

function remove(list:Array,callback:Function):Array {
    for(var i:int = list.length - 1; i >= 0; i--) {
        if(!callback(list[i])) {
            list.splice(i,1);
        }
    }
    return list;
}

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:

var arr:Array = [1,2,9,10,455];
trace(arr);

function removeCallback(item:Number):Boolean {
    return item < 10;
}
remove(arr,removeCallback);
trace(arr);

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 like filter, 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.

世界如花海般美丽 2024-10-02 12:25:21

顺便说一句,您可以使用字符串作为数组的索引,然后您可以安全地使用“delete”关键字从数组的“中间”内部删除对象(在这种情况下实际上没有“中间”:) 。
例如:

var arr:Array = new Array();

arr['o1'] = new Object();
arr['o1'].someproperty = true;
arr['o2'] = new Object();
arr['o2'].someproperty = true;
arr['o3'] = new Object();
arr['o3'].someproperty = true;

trace (arr['o2'].someproperty);
//Returns 'true'
trace (arr['o2']);
//Returns '[object Object]'

delete arr['o2'];
trace (arr['o2']);
//Returns 'undefined'
trace (arr['o2'].someproperty);
//Returns 'TypeError: Error #1010: A term is undefined and has no properties.'

缺点是你无法知道数组的长度(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.:

var arr:Array = new Array();

arr['o1'] = new Object();
arr['o1'].someproperty = true;
arr['o2'] = new Object();
arr['o2'].someproperty = true;
arr['o3'] = new Object();
arr['o3'].someproperty = true;

trace (arr['o2'].someproperty);
//Returns 'true'
trace (arr['o2']);
//Returns '[object Object]'

delete arr['o2'];
trace (arr['o2']);
//Returns 'undefined'
trace (arr['o2'].someproperty);
//Returns 'TypeError: Error #1010: A term is undefined and has no properties.'

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...

撩动你心 2024-10-02 12:25:21

这是一个通用函数,它将执行您想要的操作:

public static function removeItem(array: Array, propertyName: String, value: String): Array
{
    var newArray: Array = [];
    for (var index: int = 0; index < array.length; index++) {
        var item: Object = array[index];
        if (item && item.hasOwnProperty(propertyName)) {
            if (item[propertyName] != value)
                newArray.push(item);
        }
    }
    return newArray;
}

Here is a generic function which will do what you want:

public static function removeItem(array: Array, propertyName: String, value: String): Array
{
    var newArray: Array = [];
    for (var index: int = 0; index < array.length; index++) {
        var item: Object = array[index];
        if (item && item.hasOwnProperty(propertyName)) {
            if (item[propertyName] != value)
                newArray.push(item);
        }
    }
    return newArray;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文