Array

  • Revision slug: JavaScript/Reference/Global_Objects/Array
  • Revision title: Array
  • Revision id: 27708
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[ja:Core JavaScript 1.5 Reference:Global Objects:Array]]

Revision Content

Summary

Core Object

Lets you work with arrays.

Created by

The Array object constructor:

new Array(arrayLength)
new Array(element0, element1, ..., elementN)

An array literal:

[element0, element1, ..., elementN]

Parameters

arrayLength 
The initial length of the array. You can access this value using the length property. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
elementN 
A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.

Description

An array is an ordered set of values associated with a single variable name. Note that you shouldn't use it as an associative array, use Object instead.

The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:

coffees = ["French Roast", "Columbian", "Kona"];

You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:

myArray = new Array("Hello", myVar, 3.14159);

Indexing an array

You index an array by its ordinal number. For example, assume you define the following array:

myArray = new Array("Wind", "Rain", "Fire");

You can then refer to the elements as thus:

  • myArray{{mediawiki.external(0)}} is the first element
  • myArray{{mediawiki.external(1)}} is the second element
  • myArray{{mediawiki.external(2)}} is the third element

Specifying a single parameter

When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The following code creates an array of five elements:

billingMethod = new Array(5);

The behavior of the Array constructor depends on whether the single parameter is a number.

  • If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.
  • If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.

The following code creates an array of length 25, then assigns values to the first three elements:

musicTypes = new Array(25);
musicTypes[0] = "R&B";
musicTypes[1] = "Blues";
musicTypes[2] = "Jazz";

Increasing the array length indirectly

An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.

colors = new Array();
colors[99] = "midnightblue";

Creating an array using the result of a match

The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:

// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case

myRe = /d(b+)(d)/i;
myArray = myRe.exec("cdbBdbsbz");

The properties and elements returned from this match are as follows:

Property/Element Description Example
input A read-only property that reflects the original string against which the regular expression was matched. cdbBdbsbz
index A read-only property that is the zero-based index of the match in the string. 1
{{mediawiki.external(0)}} A read-only element that specifies the last matched characters. dbBd
{{mediawiki.external(1)}}, ...{{mediawiki.external('n')}} Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. {{mediawiki.external(1)}}: bB
{{mediawiki.external(2)}}: d

Behavior in older versions

JavaScript 1.2

When a single parameter was given to the Array constructor in an interpreter executing code as JavaScript 1.2, a single-element array was returned. For example, new Array(5) created a one-element array with the first element being 5.

JavaScript 1.0

Indexing an array by its ordinal number was required; for example:

myArray[0].

Properties

constructor
Specifies the function that creates an object's prototype.
index
For an array created by a regular expression match, the zero-based index of the match in the string.
input
For an array created by a regular expression match, reflects the original string against which the regular expression was matched.
length
Reflects the number of elements in an array.
prototype
Allows the addition of properties to all objects.

Methods

Mutator methods

These methods modify the array:

pop
Removes the last element from an array and returns that element.
push
Adds one or more elements to the end of an array and returns the new length of the array.
reverse
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
shift
Removes the first element from an array and returns that element.
sort
Sorts the elements of an array.
splice
Adds and/or removes elements from an array.
unshift
Adds one or more elements to the front of an array and returns the new length of the array.

Accessor methods

need a better name...

These methods do not modify the array and return some representation of the array.

concat
Returns a new array comprised of this array joined with other array(s) and/or value(s).
indexOf
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
join
Joins all elements of an array into a string.
lastIndexOf
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
slice
Extracts a section of an array and returns a new array.
toSource
Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource method.
toString
Returns a string representing the array and its elements. Overrides the Object.toString method.
valueOf
Returns the primitive value of the array. Overrides the Object.valueOf method.

Iteration methods

Several methods take as arguments functions to be called back while processing the array. When these methods are called, the length of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. The specific behaviour of these methods in such cases is not always well-defined, and should not be relied upon.

filter
Creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach
Calls a function for each element in the array.
every
Returns true if every element in this array satisfies the provided testing function.
map
Creates a new array with the results of calling a provided function on every element in this array.
some
Returns true if at least one element in this array satisfies the provided testing function.

Generic methods

Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a length property, and which can usefully be accessed using numeric property names (as with array{{mediawiki.external(5)}} indexing).

TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like JavaArray or String.

Some methods, such as join, only read the length and numeric properties of the object they are called on. Others, like reverse, require that the object's numeric properties and length be mutable; these methods can therefore not be called on objects like String, which does not permit its length property or synthesized numeric properties to be set.

The methods that work on any Array-like object and do not need to alter length or numeric properties are:

The methods that alter the length or numeric properties of the object they are called on are:

This example shows how to use map on a string object to get an array of bytes in the ASCII encoding representing the character values:

var a = Array.prototype.map.call("Hello World", 
                                 function(x) { return x.charCodeAt(0); })
// a now equals [72,101,108,108,111,32,87,111,114,108,100]

Examples

Example: Creating an Array

The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray{{mediawiki.external(0)}} and msgArray{{mediawiki.external(99)}}, changing the length of the array to 100.

var msgArray = new Array();
msgArray[0] = "Hello";
msgArray[99] = "world";
// The following statement is true,
// because defined msgArray[99] element.
if (msgArray.length == 100)
   myVar = "The length is 100.";

Example: Creating a Two-dimensional Array

The following creates a two-dimensional array and assigns the results to myVar.

var myVar = "Multidimensional array test; ";
a = new Array(4);
for (var i = 0; i < 4; i++) {
   a[i] = new Array(4);
   for (var j = 0; j < 4; j++) {
      a[i][j] = "[" + i + "," + j + "]";
   }
}
for (var i = 0; i < 4; i++) {
   str = "Row " + i + ":";
   for (var j = 0; j < 4; j++) {
      str += a[i][j];
   }
   myVar += str + "; ";
}

The following string is assigned to myVar (line breaks are used here for readability):

Multidimensional array test;
Row 0: [0,0][0,1][0,2][0,3];
Row 1: [1,0][1,1][1,2][1,3];
Row 2: [2,0][2,1][2,2][2,3];
Row 3: [3,0][3,1][3,2][3,3];
{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>Lets you work with arrays.
</p>
<h3 name="Created_by"> Created by </h3>
<p>The <code>Array</code> object constructor:
</p>
<pre class="eval">new Array(<i>arrayLength</i>)
new Array(<i>element0</i>, <i>element1</i>, ..., <i>elementN</i>)
</pre>
<p>An array literal:
</p>
<pre class="eval">[<i>element0</i>, <i>element1</i>, ..., <i>elementN</i>]
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>arrayLength</code> </dt><dd> The initial length of the array. You can access this value using the length property. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
</dd></dl>
<dl><dt> <code>element<i>N</i></code> </dt><dd> A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>An array is an ordered set of values associated with a single variable name. Note that you <a class="external" href="http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/">shouldn't use it as an associative array</a>, use <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a> instead.
</p><p>The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:
</p>
<pre class="eval">coffees = ["French Roast", "Columbian", "Kona"];
</pre>
<p>You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:
</p>
<pre class="eval">myArray = new Array("Hello", myVar, 3.14159);
</pre>
<h4 name="Indexing_an_array"> Indexing an array </h4>
<p>You index an array by its ordinal number. For example, assume you define the following array:
</p>
<pre class="eval">myArray = new Array("Wind", "Rain", "Fire");
</pre>
<p>You can then refer to the elements as thus:
</p>
<ul><li> <code>myArray{{mediawiki.external(0)}}</code> is the first element
</li><li> <code>myArray{{mediawiki.external(1)}}</code> is the second element
</li><li> <code>myArray{{mediawiki.external(2)}}</code> is the third element
</li></ul>
<h4 name="Specifying_a_single_parameter"> Specifying a single parameter </h4>
<p>When you specify a single numeric parameter with the <code>Array</code> constructor, you specify the initial length of the array. The following code creates an array of five elements:
</p>
<pre class="eval">billingMethod = new Array(5);
</pre>
<p>The behavior of the <code>Array</code> constructor depends on whether the single parameter is a number.
</p>
<ul><li> If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.
</li></ul>
<ul><li> If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.
</li></ul>
<p>The following code creates an array of length 25, then assigns values to the first three elements:
</p>
<pre class="eval">musicTypes = new Array(25);
musicTypes[0] = "R&amp;B";
musicTypes[1] = "Blues";
musicTypes[2] = "Jazz";
</pre>
<h4 name="Increasing_the_array_length_indirectly"> Increasing the array length indirectly </h4>
<p>An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.
</p>
<pre class="eval">colors = new Array();
colors[99] = "midnightblue";
</pre>
<h4 name="Creating_an_array_using_the_result_of_a_match"> Creating an array using the result of a match </h4>
<p>The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:
</p>
<pre class="eval">// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case

myRe = /d(b+)(d)/i;
myArray = myRe.exec("cdbBdbsbz");
</pre>
<p>The properties and elements returned from this match are as follows:
</p>
<table class="fullwidth-table">

<tbody><tr>
<td class="header">Property/Element
</td><td class="header">Description
</td><td class="header">Example
</td></tr>

<tr>
<td><code>input</code>
</td><td>A read-only property that reflects the original string against which the regular expression was matched.
</td><td>cdbBdbsbz
</td></tr>

<tr>
<td><code>index</code>
</td><td>A read-only property that is the zero-based index of the match in the string.
</td><td>1
</td></tr>

<tr>
<td><code>{{mediawiki.external(0)}}</code>
</td><td>A read-only element that specifies the last matched characters.
</td><td>dbBd
</td></tr>

<tr>
<td><code>{{mediawiki.external(1)}}, ...{{mediawiki.external('n')}}</code>
</td><td>Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited.
</td><td>{{mediawiki.external(1)}}: bB<br>{{mediawiki.external(2)}}: d
</td></tr>
</tbody></table>
<h3 name="Behavior_in_older_versions"> Behavior in older versions </h3>
<h4 name="JavaScript_1.2"> JavaScript 1.2 </h4>
<p>When a single parameter was given to the Array constructor in an interpreter executing code as JavaScript 1.2, a single-element array was returned. For example, <code>new Array(5)</code> created a one-element array with the first element being 5.
</p>
<h4 name="JavaScript_1.0"> JavaScript 1.0 </h4>
<p>Indexing an array by its ordinal number was required; for example:
</p>
<pre class="eval"><code>myArray[0]</code>.
</pre>
<h3 name="Properties"> Properties </h3>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/constructor">constructor</a></dt><dd> Specifies the function that creates an object's prototype.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/index">index</a></dt><dd> For an array created by a regular expression match, the zero-based index of the match in the string.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/input">input</a></dt><dd>  For an array created by a regular expression match, reflects the original string against which the regular expression was matched.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/length">length</a></dt><dd> Reflects the number of elements in an array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/prototype">prototype</a></dt><dd> Allows the addition of properties to all objects.
</dd></dl>
</dd></dl>
<h3 name="Methods"> Methods </h3>
<h4 name="Mutator_methods"> Mutator methods </h4>
<p>These methods modify the array:
</p>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/pop">pop</a></dt><dd> Removes the last element from an array and returns that element.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/push">push</a></dt><dd> Adds one or more elements to the end of an array and returns the new length of the array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/reverse">reverse</a></dt><dd> Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/shift">shift</a></dt><dd> Removes the first element from an array and returns that element.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort">sort</a></dt><dd> Sorts the elements of an array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice">splice</a></dt><dd> Adds and/or removes elements from an array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/unshift">unshift</a></dt><dd> Adds one or more elements to the front of an array and returns the new length of the array.
</dd></dl>
</dd></dl>
<h4 name="Accessor_methods"> Accessor methods </h4>
<p><span class="comment">need a better name...</span>
</p><p>These methods do not modify the array and return some representation of the array.
</p>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/concat">concat</a></dt><dd> Returns a new array comprised of this array joined with other array(s) and/or value(s).
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf">indexOf</a></dt><dd> Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/join">join</a></dt><dd> Joins all elements of an array into a string.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf">lastIndexOf</a></dt><dd> Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/slice">slice</a></dt><dd> Extracts a section of an array and returns a new array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/toSource">toSource</a></dt><dd> Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource method.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/toString">toString</a></dt><dd> Returns a string representing the array and its elements. Overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/valueOf">valueOf</a></dt><dd> Returns the primitive value of the array.  Overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a> method.
</dd></dl>
</dd></dl>
<h4 name="Iteration_methods"> Iteration methods </h4>
<p>Several methods take as arguments functions to be called back while processing the array. When these methods are called, the <code>length</code> of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. The specific behaviour of these methods in such cases is not always well-defined, and should not be relied upon.
</p>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter">filter</a></dt><dd> Creates a new array with all of the elements of this array for which the provided filtering function returns true.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach">forEach</a></dt><dd> Calls a function for each element in the array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every">every</a></dt><dd> Returns true if every element in this array satisfies the provided testing function.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map">map</a></dt><dd> Creates a new array with the results of calling a provided function on every element in this array.
</dd></dl>
</dd></dl>
<dl><dd><dl><dt> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some">some</a></dt><dd> Returns true if at least one element in this array satisfies the provided testing function.
</dd></dl>
</dd></dl>
<h4 name="Generic_methods"> Generic methods </h4>
<p>Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays.  That is, they can be used on any object which has a <code>length</code> property, and which can usefully be accessed using numeric property names (as with <code>array{{mediawiki.external(5)}}</code> indexing).
</p><p>TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/JavaArray">JavaArray</a> or <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String">String</a>.
</p><p>Some methods, such as <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/join">join</a>, only read the <code>length</code> and numeric properties of the object they are called on.  Others, like <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/reverse">reverse</a>, require that the object's numeric properties and <code>length</code> be mutable; these methods can therefore not be called on objects like <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String">String</a>, which does not permit its <code>length</code> property or synthesized numeric properties to be set.
</p><p>The methods that work on any Array-like object and do <b>not</b> need to alter <code>length</code> or numeric properties are:
</p>
<ul><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/concat">concat</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every">every</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter">filter</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach">forEach</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf">indexOf</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/join">join</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf">lastIndexOf</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map">map</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/slice">slice</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some">some</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/toSource">toSource</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/toString">toString</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/valueOf">valueOf</a>
</li></ul>
<p>The methods that alter the <code>length</code> or numeric properties of the object they are called on are:
</p>
<ul><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/pop">pop</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/push">push</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/reverse">reverse</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/shift">shift</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort">sort</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice">splice</a>
</li><li> <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/unshift">unshift</a>
</li></ul>
<p>This example shows how to use <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map">map</a> on a <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/String">string</a> object to get an array of bytes in the ASCII encoding representing the character values:
</p>
<pre>var a = Array.prototype.map.call("Hello World", 
                                 function(x) { return x.charCodeAt(0); })
// a now equals [72,101,108,108,111,32,87,111,114,108,100]
</pre>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Creating_an_Array"> Example: Creating an Array </h4>
<p>The following example creates an array, <code>msgArray</code>, with a length of 0, then assigns values to <code>msgArray{{mediawiki.external(0)}}</code> and <code>msgArray{{mediawiki.external(99)}}</code>, changing the length of the array to 100.
</p>
<pre class="eval">var msgArray = new Array();
msgArray[0] = "Hello";
msgArray[99] = "world";
// The following statement is true,
// because defined msgArray[99] element.
if (msgArray.length == 100)
   myVar = "The length is 100.";
</pre>
<h4 name="Example:_Creating_a_Two-dimensional_Array"> Example: Creating a Two-dimensional Array </h4>
<p>The following creates a two-dimensional array and assigns the results to <code>myVar</code>.
</p>
<pre class="eval">var myVar = "Multidimensional array test; ";
a = new Array(4);
for (var i = 0; i &lt; 4; i++) {
   a[i] = new Array(4);
   for (var j = 0; j &lt; 4; j++) {
      a[i][j] = "[" + i + "," + j + "]";
   }
}
for (var i = 0; i &lt; 4; i++) {
   str = "Row " + i + ":";
   for (var j = 0; j &lt; 4; j++) {
      str += a[i][j];
   }
   myVar += str + "; ";
}
</pre>
<p>The following string is assigned to <code>myVar</code> (line breaks are used here for readability):
</p>
<pre class="eval">Multidimensional array test;
Row 0: [0,0][0,1][0,2][0,3];
Row 1: [1,0][1,1][1,2][1,3];
Row 2: [2,0][2,1][2,2][2,3];
Row 3: [3,0][3,1][3,2][3,3];
</pre>{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array" } ) }}
Revert to this revision