Flash / Flex / ActionScript/Array/splice
Содержание
- 1 Extracting Subsets of Array Elements
- 2 Inserting Elements into an Array: arrayName.splice(startingIndex, numberOfElementsToDelete, element1[,...elementN]);
- 3 Removing Elements from Within an Array
- 4 splice( ) can both remove elements from and add elements to an array.
- 5 splice( ) returns a new array containing the deleted elements
- 6 The splice( ) method can add elements to, or remove elements from, an array.
- 7 Use slice( ) instead of concat( ) to create a copy of an array
- 8 When you remove elements from an array in a for statement, you need to change the value of the index variable accordingly
Extracting Subsets of Array Elements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var aEmployeesAll:Array = ["A", "P", "C", "H",
"Gilberto", "Mary", "Ayla", "Riad"];
var aEmployeesExec:Array = aEmployeesAll.slice(0, 4);
var aEmployeesNew:Array = aEmployeesAll.slice(4, 6);
var aEmployeesStaff:Array = aEmployeesAll.slice(6);
trace(aEmployeesExe.toString());
trace(aEmployeesNew.toString());
trace(aEmployeesStaff.toString());
}
}
}
A,P,C,H
G,M
A,R
Inserting Elements into an Array: arrayName.splice(startingIndex, numberOfElementsToDelete, element1[,...elementN]);
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var aEmployees:Array = ["A", "P", "C", "H"];
aEmployees.splice(3, 0, "R", "H", "L");
trace(aEmployees.toString()); // A,P,C,R,H,L,H
}
}
}
Removing Elements from Within an Array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var aEmployees:Array = ["A", "P", "C", "H"];
var aRemovedEmployees:Array = aEmployees.splice(2, 2);
trace(aEmployees.toString());
trace(aRemovedEmployees.toString());
}
}
}
//A,P
//C,H
splice( ) can both remove elements from and add elements to an array.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c", "d", "e", "f"];
letters.splice(1, 3);
letters.splice(1);
trace(letters); // a
}
}
}
splice( ) returns a new array containing the deleted elements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c", "d"];
var deleted:Array = letters.splice(0, 2);
for (var i:int = 0; i < deleted.length; i++) {
trace(deleted[i]);
}
}
}
}
The splice( ) method can add elements to, or remove elements from, an array.
theArray.splice(startIndex, deleteCount, item1, item2,...itemn)
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var months:Array = new Array("January", "Friday",
"April", "May", "Sunday", "Monday", "July");
months.splice(1,1);
months.splice(1, 0, "February", "March");
months.splice(5, 2, "June");
months.splice(3); // months is now: ["January", "February", "March"]
trace(months); //January,February,March
}
}
}
Use slice( ) instead of concat( ) to create a copy of an array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c"];
var newLetters:Array = letters.slice(0);
trace(letters); // Displays: "a,b,c"
trace(newLetters); // Displays: "a,b,c"
letters = ["d", "e", "f"];
trace(letters); // Displays: "d,e,f"
trace(newLetters); // Displays: "a,b,c"
}
}
}
a,b,c
a,b,c
d,e,f
a,b,c
When you remove elements from an array in a for statement, you need to change the value of the index variable accordingly
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var numbers:Array = new Array(4, 10);
numbers[4] = 1;
trace(numbers); // Displays: 4,10,undefined,undefined,1
for(var i:int = 0; i < numbers.length; i++) {
if(numbers[i] == undefined) {
numbers.splice(i, 1);
}
}
trace(numbers); // Displays: 4,10,undefined,1
}
}
}
// 4,10,,,1
// 4,10,,1