5

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.

4 Answers 4

14

Use

if(array.indexOf(obj) != -1)
array.splice(array.indexOf(obj),1); 
Sign up to request clarification or add additional context in comments.

2 Comments

This will remove the last element of the array if the item is not in the array!
thanks @Pimgd... i was a bit careless.... you can do a check before executing this statement... editting the answer.....
10

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.

Comments

2

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

2 Comments

This is actually the same thing as using an Object.
If you're doing this it's better to use an Object instead of an Array
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.