Condividi tramite


Array Type Extensions

Provides extensions to the base ECMAScript (JavaScript) Array functionality by adding static methods.

Namespace: None. This type extension is global and not part of a namespace.

Inherits: Array

var arrayVar = new Array("Saturn","Mars","Jupiter");
Array.add(arrayVar, "Earth");

Member Extensions

Name

Description

Array.add Function

Adds an element to the end of an Array object.

Array.addRange Function

Copies all the elements of the specified array to the end of an Array object.

Array.clear Function

Removes all elements from an Array object.

Array.clone Function

Creates a shallow copy of an Array object.

Array.contains Function

Determines whether an element is in an Array object.

Array.dequeue Function

Removes the first element from an Array object.

Array.enqueue Function

Adds an element to the end of an Array object.

Note

Use the add function instead of the Array.enqueue function.

Array.forEach Function

Performs a specified action on each element of an Array object.

Array.indexOf Function

Searches for the specified element of an Array object and returns its index.

Array.insert Function

Inserts a value at the specified location in an Array object.

Array.parse Function

Creates an Array object from a string representation.

Array.remove Function

Removes the first occurrence of an element in an Array object.

Array.removeAt Function

Removes an element at the specified location in an Array object.

Remarks

Array extensions are part of the Microsoft Ajax Library. They add static methods to the JavaScript Array object for additional functionality.

For more information about the JavaScript object that these static methods extend and about its constructor, see Array Object in the Language Reference.

Example

The following example shows how to create a new Array object and invoke the Microsoft Ajax Library add function to add arrays as an element to a single array. The multidimensional array is then passed to a function that displays the array as a table in the document.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1">
        </asp:ScriptManager>

        <div id="results">
        </div>

        <script type="text/javascript">

        // Create and display a table based on array content.
        function displayTable(arrayTable, element) 
        {
            var tableMarkup;
            tableMarkup = "<table border=on>";
            var rows = arrayTable[0][0].length;

            for(x=0; x<=rows; x++)
            {
               tableMarkup += "<tr>";
               var columns = arrayTable[x].length - 1;
               for(y=0; y<=columns; y++)
               {
                    tableMarkup += "<td>" + arrayTable[x][y] + "</td>";
               }
               tableMarkup += "</tr>";
            }
            tableMarkup += "</table>";

            element.innerHTML += tableMarkup;

            // Clean up.
            Array.clear(arrayTable);
        }

        // Create table data.
        function createTableData() 
        {
               var costsArray = [];

               var headerRow = new Array("ID", "Name", "Costs");
               var firstRow = new Array("1", "ruler", "1.30");
               var secondRow = new Array("2", "binder", "4.75");

               Array.add(costsArray, headerRow);
               Array.add(costsArray, firstRow);
               Array.add(costsArray, secondRow);

               return costsArray;
        }  

        var myTable = createTableData();
        var element = $get("results");
        displayTable(myTable, element);

    </script>
    </form>
</body>
</html>

See Also

Reference

Array Object

new Operator

Array Type Functions