简体   繁体   中英

AS3 How to remove object from array

I have a array called 'BODY_STAT_ARRAY' and in this array it holds a object called 'body_stat'. This array needs to increase a decrease in size all the time, how do i remove the last object from the array, here is a example which doesn't work

if(BODY_STAT_ARRAY.length > target_size)
{ BODY_STAT_ARRAY.slice(BODY_STAT_ARRAY.indexOf(BODY_STAT_ARRAY[BODY_STAT_ARRAY.length-1]),1) // this line above should remove the last object in the array }

So where am I going wrong, how do you make it work.

If you can help, I would love to find out.

从数组中删除最后一个对象的最简单方法是调用pop

BODY_STAT_ARRAY.pop();

Slice will not modify the Original Array . It will give an another Array with the defined range .

Only Splice will modify the original Array , so as to remove or add elements .

var a:Array = [1,2,3,4];

a.slice(1,1);
trace(a);           // Output is : 1,2,3,4

a.splice(1,1);
trace(a);           //  Output is : 1,3,4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM