Skip to Content
ActionScript: The Definitive Guide
book

ActionScript: The Definitive Guide

by Colin Moock
May 2001
Intermediate to advanced
720 pages
23h 24m
English
O'Reilly Media, Inc.
Content preview from ActionScript: The Definitive Guide

Adding Elements to an Array

You can add elements to an array by specifying a value for a new element, increasing the array’s length property, or using one of the built-in array functions.

Adding New Elements Directly

We can add a new element to an existing array at a specific index by simply assigning a value to that element:

// Create an array, and assign it three values
var myList = ["apples", "oranges", "pears"];

// Add a fourth value
myList[3] = "tangerines";

The new element does not need to be placed immediately after the last element of the old array. If we place the new element more than one element beyond the end of the array, ActionScript automatically creates empty elements for the intervening indexes:

// Leave indexes 4 to 38 empty
myList[39] = "grapes";

trace (myList[12]); // Display is empty because element 12 is undefined

Adding New Elements with the length Property

To extend an array without assigning values to new elements, we can simply increase the length property and ActionScript will add enough elements to reach that length:

// Create an array with three elements
var myColors = ["green", "red", "blue"];

// Add 47 empty elements, numbered 3 through 49, to the array
myColors.length = 50;

You might use this approach to create a number of empty elements to hold some data you expect to accumulate, such as student test scores.

Adding New Elements with Array Methods

We can use built-in array methods to handle more complex element-addition scenarios. (We’ll learn in Chapter 12 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Actionscript Cookbook

Actionscript Cookbook

Joey Lott

Publisher Resources

ISBN: 1565928520Catalog PageErrata