AS3 Remove an element from an array (of objects) in the general case

Is there a way to generally remove an object from an array?
(maybe not use array.filter or create 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 

Im currently using array.splice () to delete an object

 for (var i:Number=0; i < arr.length; i++) { if (arr[i].Name == element) { arr.splice(i, 1); } } 

I want to write removeElement so that I can use it for different types of objects.
removeElement is currently becoming dependent on implantation ..
Suppose if I want to delete a file from an array of files with the given file name .. wud have to write "removeElement" again, changing the criteria.

Maybe I can change the criteria for validating the criteria? Example:

 arr= removeElement("Site","http://www.mysite.com/6",arr) 

will remove the object from arr, whose property "Site" is equal to "http://www.mysite.com/6", (using the example above)

 ie. removeElement(criteria:object,criteria_value(s):object,arr) 

Thanks to everyone.

+4
source share
4 answers

I think the most flexible approach is Array::filter . It depends on the caller to determine whether the item should be filtered out of the list or not, through a function callback.

Now, if you want to do this in place, you can 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 a list, as it may be convenient if you want to bind calls, but it acts on the array you passed in, instead of creating a new one.

Also note that it moves backward. Otherwise, splicing will give you dummy results.

You can use it as follows:

 var arr:Array = [1,2,9,10,455]; trace(arr); function removeCallback(item:Number):Boolean { return item < 10; } remove(arr,removeCallback); trace(arr); 

Thus, you are not limited to equality (or inequality). The caller determines whether the element should be saved or deleted by returning true or false respectively (to match the filter ). Thus, it is almost similar to filter , except that it works in place. If you want, you can also keep the same interface for the callback (passing the index of the element and a reference to the original array) to make it more consistent.

+10
source

Using

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

By the way, you can use strings as indices for an array, and then you can safely use the keyword "delete" to remove an object from the "middle" (in fact, there is no "average" in this situation :) array.

eg:.

 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 that you won’t be able to find out the length of the array (arr.length will return 0), but you can, of course, track it yourself ...

+2
source

Here is a generic function that 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; } 
+1
source

All Articles