X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/6746dc89c47ed01b165cc1152533605f97eb8e8d..f562e4c6e5fac7bcb445985b99acbea4d706e6f0:/docs/output/Array.js diff --git a/docs/output/Array.js b/docs/output/Array.js new file mode 100644 index 00000000..06358a04 --- /dev/null +++ b/docs/output/Array.js @@ -0,0 +1 @@ +Ext.data.JsonP.Array({"tagname":"class","html":"
Files
In JavaScript, the Array
property of the global object is a constructor for\narray instances.
An array is a JavaScript object. Note that you shouldn't use it as an\nassociative array, use Object instead.
\n\nThe following example creates an array, msgArray, with a length of 0, then assigns values to\nmsgArray[0] and msgArray[99], changing the length of the array to 100.
\n\nvar msgArray = new Array();\nmsgArray[0] = \"Hello\";\nmsgArray[99] = \"world\";\n\nif (msgArray.length == 100)\nprint(\"The length is 100.\");\n
\n\nThe following creates chess board as a two dimensional array of strings. The first move is made by\ncopying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.
\n\nvar board =\n[ ['R','N','B','Q','K','B','N','R'],\n['P','P','P','P','P','P','P','P'],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n['p','p','p','p','p','p','p','p'],\n['r','n','b','q','k','b','n','r']];\nprint(board.join('\\n') + '\\n\\n');\n\n// Move King's Pawn forward 2\nboard[4][4] = board[6][4];\nboard[6][4] = ' ';\nprint(board.join('\\n'));\n
\n\nHere is the output:
\n\nR,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\np,p,p,p,p,p,p,p\nr,n,b,q,k,b,n,r\n\nR,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , ,p, , ,\n , , , , , , ,\np,p,p,p, ,p,p,p\nr,n,b,q,k,b,n,r\n
\n\nArray elements are nothing less than object properties, so they are accessed as such.
\n\nvar myArray = new Array(\"Wind\", \"Rain\", \"Fire\");\nmyArray[0]; // \"Wind\"\nmyArray[1]; // \"Rain\"\n// etc.\nmyArray.length; // 3\n\n// Even if indices are properties, the following notation throws a syntax error\nmyArray.2;\n\n// It should be noted that in JavaScript, object property names are strings. Consequently,\nmyArray[0] === myArray[\"0\"];\nmyArray[1] === myArray[\"1\"];\n// etc.\n\n// However, this should be considered carefully\nmyArray[02]; // \"Fire\". The number 02 is converted as the \"2\" string\nmyArray[\"02\"]; // undefined. There is no property named \"02\"\n
\n\nAn array's length property and numerical properties are connected. Here is some\ncode explaining how this relationship works.
\n\nvar a = [];\n\na[0] = 'a';\nconsole.log(a[0]); // 'a'\nconsole.log(a.length); // 1\n\na[1] = 32;\nconsole.log(a[1]); // 32\nconsole.log(a.length); // 2\n\na[13] = 12345;\nconsole.log(a[13]); // 12345\nconsole.log(a.length); // 14\n\na.length = 10;\nconsole.log(a[13]); // undefined, when reducing the length elements after length+1 are removed\nconsole.log(a.length); // 10\n
\n\nThe result of a match between a regular expression and a string can create an array.\nThis array has properties and elements that provide information about the match. An\narray is the return value of RegExp.exec, String.match, and String.replace. To help\nexplain these properties and elements, look at the following example and then refer\nto the table below:
\n\n// Match one d followed by one or more b's followed by one d\n// Remember matched b's and the following d\n// Ignore case\n\nvar myRe = /d(b+)(d)/i;\nvar myArray = myRe.exec(\"cdbBdbsbz\");\n
\n\nThe properties and elements returned from this match are as follows:
\n\n\n | Property/Element | \nDescription | \nExample | \n
---|---|---|---|
\n | input | \nA read-only property that reflects the original string against which the | \ncdbBdbsbz | \n
\n | \n | regular expression was matched. | \n\n |
\n | index | \nA read-only property that is the zero-based index of the match in the string. | \n1 | \n
\n | [0] | \nA read-only element that specifies the last matched characters. | \ndbBd | \n
\n | [1], ...[n] | \nRead-only elements that specify the parenthesized substring matches, if included in | \n[1]: bB [2]: d | \n
\n | \n | the regular expression. The number of possible parenthesized substrings is unlimited. | \n\n |
Reflects the number of elements in an array.
\n\nThe value of the length
property is an integer with a positive sign and a value less than 2 to the 32\npower (232).
You can set the length
property to truncate an array at any time. When you extend an array by changing\nits length
property, the number of actual elements does not increase; for example, if you set length
\nto 3 when it is currently 2, the array still contains only 2 elements.
In the following example the array numbers is iterated through by looking at the length
property to see\nhow many elements it has. Each value is then doubled.
var numbers = [1,2,3,4,5];\nfor (var i = 0; i < numbers.length; i++) {\n numbers[i] *= 2;\n}\n// numbers is now [2,4,6,8,10];\n
\n\nThe following example shortens the array statesUS
to a length of 50 if the current length
is greater\nthan 50.
if (statesUS.length > 50) {\n statesUS.length=50\n}\n
\nReturns a new array comprised of this array joined with other array(s) and/or value(s).
\n\nconcat
creates a new array consisting of the elements in the this
object on which it is called,\nfollowed in order by, for each argument, the elements of that argument (if the argument is an\narray) or the argument itself (if the argument is not an array).
concat
does not alter this
or any of the arrays provided as arguments but instead returns a\n\"one level deep\" copy that contains copies of the same elements combined from the original arrays.\nElements of the original arrays are copied into the new array as follows:\nObject references (and not the actual object): concat
copies object references into the new\narray. Both the original and new array refer to the same object. That is, if a referenced object is\nmodified, the changes are visible to both the new and original arrays.\nStrings and numbers (not String and Number objects): concat
copies the values of\nstrings and numbers into the new array.
Any operation on the new array will have no effect on the original arrays, and vice versa.
\n\nThe following code concatenates two arrays:
\n\nvar alpha = [\"a\", \"b\", \"c\"];\nvar numeric = [1, 2, 3];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3]; alpha and numeric are unchanged\nvar alphaNumeric = alpha.concat(numeric);\n
\n\nThe following code concatenates three arrays:
\n\nvar num1 = [1, 2, 3];\nvar num2 = [4, 5, 6];\nvar num3 = [7, 8, 9];\n\n// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged\nvar nums = num1.concat(num2, num3);\n
\n\nThe following code concatenates three values to an array:
\n\nvar alpha = ['a', 'b', 'c'];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3], leaving alpha unchanged\nvar alphaNumeric = alpha.concat(1, [2, 3]);\n
\nArrays and/or values to concatenate to the resulting array.
\nNew array.
\nJoins all elements of an array into a string.
\n\nThe string conversions of all array elements are joined into one string.
\n\nThe following example creates an array, a
, with three elements, then joins the array three times:\nusing the default separator, then a comma and a space, and then a plus.
var a = new Array(\"Wind\",\"Rain\",\"Fire\");\nvar myVar1 = a.join(); // assigns \"Wind,Rain,Fire\" to myVar1\nvar myVar2 = a.join(\", \"); // assigns \"Wind, Rain, Fire\" to myVar2\nvar myVar3 = a.join(\" + \"); // assigns \"Wind + Rain + Fire\" to myVar3\n
\nSpecifies a string to separate each element of the array. The separator\nis converted to a string if necessary. If omitted, the array elements are separated with a comma.
\nA string of the array elements.
\nThe pop method removes the last element from an array and returns that value to the caller.
\n\npop
is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.
var myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nvar popped = myFish.pop();\nalert(popped); // Alerts 'surgeon'\n
\nThe last element in the array
\nAdds one or more elements to the end of an array and returns the new length of the array.
\n\npush
is intentionally generic. This method can be called or applied to objects resembling\narrays. The push method relies on a length property to determine where to start inserting\nthe given values. If the length property cannot be converted into a number, the index used\nis 0. This includes the possibility of length being nonexistent, in which case length will\nalso be created.
The only native, array-like objects are strings, although they are not suitable in\napplications of this method, as strings are immutable.
\n\nThe following code creates the sports array containing two elements, then appends two elements\nto it. After the code executes, sports contains 4 elements: \"soccer\", \"baseball\", \"football\"\nand \"swimming\".
\n\nvar sports = [\"soccer\", \"baseball\"];\nsports.push(\"football\", \"swimming\");\n
\nThe elements to add to the end of the array.
\nThe new length property of the object upon which the method was called.
\nReverses the order of the elements of an array -- the first becomes the last, and the\nlast becomes the first.
\n\nThe reverse method transposes the elements of the calling array object in place, mutating the\narray, and returning a reference to the array.
\n\nThe following example creates an array myArray, containing three elements, then reverses the array.
\n\nvar myArray = [\"one\", \"two\", \"three\"];\nmyArray.reverse();\n
\n\nThis code changes myArray so that:
\n\nA reference to the array
\nRemoves the first element from an array and returns that element.
\n\nThe shift
method removes the element at the zeroeth index and shifts the values at consecutive\nindexes down, then returns the removed value.
shift
is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length
property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.
The following code displays the myFish
array before and after removing its first element. It also\ndisplays the removed element:
// assumes a println function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprintln(\"myFish before: \" + myFish);\nvar shifted = myFish.shift();\nprintln(\"myFish after: \" + myFish);\nprintln(\"Removed this element: \" + shifted);\n
\n\nThis example displays the following:
\n\nmyFish before: angel,clown,mandarin,surgeon\nmyFish after: clown,mandarin,surgeon\nRemoved this element: angel\n
\nThe first element of the array prior to shifting.
\nExtracts a section of an array and returns a new array.
\n\nslice
does not alter the original array, but returns a new \"one level deep\" copy that contains\ncopies of the elements sliced from the original array. Elements of the original array are copied\ninto the new array as follows:\n* For object references (and not the actual object), slice
copies object references into the\nnew array. Both the original and new array refer to the same object. If a referenced object\nchanges, the changes are visible to both the new and original arrays.\n* For strings and numbers (not String and Number objects), slice
copies strings\nand numbers into the new array. Changes to the string or number in one array does not affect the\nother array.
If a new element is added to either array, the other array is not affected.
\n\nIn the following example, slice
creates a new array, newCar
, from myCar
. Both include a\nreference to the object myHonda
. When the color of myHonda
is changed to purple, both arrays\nreflect the change.
// Using slice, create newCar from myCar.\nvar myHonda = { color: \"red\", wheels: 4, engine: { cylinders: 4, size: 2.2 } };\nvar myCar = [myHonda, 2, \"cherry condition\", \"purchased 1997\"];\nvar newCar = myCar.slice(0, 2);\n\n// Print the values of myCar, newCar, and the color of myHonda\n// referenced from both arrays.\nprint(\"myCar = \" + myCar.toSource());\nprint(\"newCar = \" + newCar.toSource());\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n\n// Change the color of myHonda.\nmyHonda.color = \"purple\";\nprint(\"The new color of my Honda is \" + myHonda.color);\n\n// Print the color of myHonda referenced from both arrays.\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n
\n\nThis script writes:
\n\nmyCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2, \"cherry condition\",\n\"purchased 1997\"]\nnewCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2]\nmyCar[0].color = red\nnewCar[0].color = red\nThe new color of my Honda is purple\nmyCar[0].color = purple\nnewCar[0].color = purple\n
\nZero-based index at which to begin extraction.\nAs a negative index, start
indicates an offset from the end of the sequence. slice(-2)
extracts\nthe second-to-last element and the last element in the sequence
Zero-based index at which to end extraction. slice
extracts up to but not\nincluding end
.\nslice(1,4)
extracts the second element through the fourth element (elements indexed 1, 2, and 3).\nAs a negative index, end indicates an offset from the end of the sequence. slice(2,-1)
extracts\nthe third element through the second-to-last element in the sequence.\nIf end
is omitted, slice
extracts to the end of the sequence.
Array from the new start position up to (but not including) the specified end position.
\nSorts the elements of an array.
\n\nIf compareFunction
is not supplied, elements are sorted by converting them to strings and\ncomparing strings in lexicographic (\"dictionary\" or \"telephone book,\" not numerical) order. For\nexample, \"80\" comes before \"9\" in lexicographic order, but in a numeric sort 9 comes before 80.
If compareFunction
is supplied, the array elements are sorted according to the return value of\nthe compare function. If a and b are two elements being compared, then:\nIf compareFunction(a, b)
is less than 0, sort a
to a lower index than b
.\nIf compareFunction(a, b)
returns 0, leave a
and b
unchanged with respect to each other, but\nsorted with respect to all different elements. Note: the ECMAscript standard does not guarantee\nthis behaviour, and thus not all browsers respect this.\nIf compareFunction(a, b)
is greater than 0, sort b
to a lower index than a
.\ncompareFunction(a, b)
must always returns the same value when given a specific pair of elements a\nand b as its two arguments. If inconsistent results are returned then the sort order is undefined
So, the compare function has the following form:
\n\nfunction compare(a, b)\n{\n if (a is less than b by some ordering criterion)\n return -1;\n if (a is greater than b by the ordering criterion)\n return 1;\n // a must be equal to b\n return 0;\n}\n
\n\nTo compare numbers instead of strings, the compare function can simply subtract b
from a
:
function compareNumbers(a, b)\n{\nreturn a - b;\n}\n
\n\nThe sort() method can be conveniently used with closures:
\n\nvar numbers = [4, 2, 5, 1, 3];\nnumbers.sort(function(a, b) {\n return a - b;\n});\nprint(numbers);\n
\nSpecifies a function that defines the sort order. If omitted, the\narray is sorted lexicographically (in dictionary order) according to the string conversion of each\nelement.
\nA reference to the array
\nAdds and/or removes elements from an array.
\n\nIf you specify a different number of elements to insert than the number you're removing, the array\nwill have a different length at the end of the call.
\n\n// assumes a print function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprint(\"myFish: \" + myFish);\n\nvar removed = myFish.splice(2, 0, \"drum\");\nprint(\"After adding 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(3, 1);\nprint(\"After removing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(2, 1, \"trumpet\");\nprint(\"After replacing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(0, 2, \"parrot\", \"anemone\", \"blue\");\nprint(\"After replacing 2: \" + myFish);\nprint(\"removed is: \" + removed);\n
\n\nThis script displays:
\n\nmyFish: angel,clown,mandarin,surgeon\nAfter adding 1: angel,clown,drum,mandarin,surgeon\nremoved is:\nAfter removing 1: angel,clown,drum,surgeon\nremoved is: mandarin\nAfter replacing 1: angel,clown,trumpet,surgeon\nremoved is: drum\nAfter replacing 2: parrot,anemone,blue,trumpet,surgeon\nremoved is: angel,clown\n
\nIndex at which to start changing the array. If negative, will begin that\nmany elements from the end.
\nAn integer indicating the number of old array elements to remove. If\nhowMany
is 0, no elements are removed. In this case, you should specify at least one new element.\nIf no howMany
parameter is specified all elements after index are removed.
The elements to add to the array. If you don't specify any\nelements, splice
simply removes elements from the array.
An array containing the removed elements. If only one element is removed, an array\nof one element is returned..
\nReturns a string representing the array and its elements. Overrides the Object.prototype.toString
\nmethod.
The Array object overrides the toString
method of Object. For Array objects, the\ntoString
method joins the array and returns one string containing each array element separated by\ncommas. For example, the following code creates an array and uses toString
to convert the array\nto a string.
var monthNames = new Array(\"Jan\",\"Feb\",\"Mar\",\"Apr\");\nmyVar = monthNames.toString(); // assigns \"Jan,Feb,Mar,Apr\" to myVar\n
\n\nJavaScript calls the toString
method automatically when an array is to be represented as a text\nvalue or when an array is referred to in a string concatenation.
The array as a string.
\nAdds one or more elements to the front of an array and returns the new length of the array.
\n\nThe unshift
method inserts the given values to the beginning of an array-like object.
unshift
is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length
property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.
The following code displays the myFish array before and after adding elements to it.
\n\n// assumes a println function exists\nmyFish = [\"angel\", \"clown\"];\nprintln(\"myFish before: \" + myFish);\nunshifted = myFish.unshift(\"drum\", \"lion\");\nprintln(\"myFish after: \" + myFish);\nprintln(\"New length: \" + unshifted);\n
\n\nThis example displays the following:
\n\nmyFish before: [\"angel\", \"clown\"]\nmyFish after: [\"drum\", \"lion\", \"angel\", \"clown\"]\nNew length: 4\n
\nThe elements to add to the front of the array.
\nThe array's new length.
\n