in which elements are accessed by integers that are used to compute offsets. O Array can be very fast data structure . O JavaScript does not have this kind of array .
converts array subscripts into string that are used to make properties. O Slower that real array , but more convenient to use. O Arrays have some useful methods.
bound. O The largest integer property name plus one. O This is not necessarily the number of properties in the array. var myArray = []; myArray.length // 0 myArray[1000] = true; myArray.length // 1001
set explicitly. O making the length larger does not allocate more space. O making the length smaller will cause delete subscript greater than or equal.
string from an array. The default separator is ',‘ var a = ['a', 'b', 'c']; var c = a.join(''); // c is 'abc'; If you are assembling a string from a large number of pieces, it is usually faster to put the pieces into an array and join them than it is to concatenate the pieces with the + operator
methods make an array work like a stack. The pop method removes and returns the last element in this array. If the array is empty, it returns undefined. var a = ['a', 'b', 'c']; var c = a.pop( ); // a is ['a', 'b'] & c is 'c'
to the end of an array. It returns the new length of the array. var a = ['a', 'b', 'c']; var b = ['x', 'y', 'z']; var c = a.push(b, true); // a is ['a', 'b', 'c', ['x', 'y', 'z'], true] // c is 5;
element from an array and returns it. If the array is empty, it returns undefined. Shift is usually much slower than pop. var a = ['a', 'b', 'c']; var c = a.shift( ); // a is ['b', 'c'] & c is 'a'
copy of a portion of an array. The end parameter is optional, and the default is array.length. If either parameter is negative, array.length will be added to them. If start is greater than or equal to array.length, the result will be a new empty array. var a = ['a', 'b', 'c']; var b = a.slice(0, 1); // b is ['a'] var c = a.slice(1); // c is ['b', 'c'] var d = a.slice(1, 2); // d is ['b']
O http://jsfiddle.net/rahmani/9xKX8/ O Array sort for simple types ( int , string) O http://jsfiddle.net/rahmani/PaN9z/ O Array sort for objects O http://jsfiddle.net/rahmani/X6XX4/2/
an array, replacing them with new items. It returns an array containing the deleted elements. var a = ['a', 'b', 'c']; var r = a.splice(1, 1, 'ache', 'bug'); // a is ['a', 'ache', 'bug', 'c'] // r is ['b'] Example: Delete a record from json array http://jsfiddle.net/rahmani/7hGR9/
except that it shoves the items onto the front of this array instead of at the end. It returns the array’s new length. var a = ['a', 'b', 'c']; var r = a.unshift('?', '@'); // a is ['?', '@', 'a', 'b', 'c'] // r is 5