Array
There are two basic ways to create an array:
1, using the Array constructor
Syntax: new Array ()
Parentheses () Description:
(1) know in advance the number of arrays to save project
(2) transfer terms to be included in the array Array constructor
2, using array literal notation
A pair of brackets comprising an array of items [] represents, to a plurality of array entries separated by commas.
And reading set values, using square brackets [] index and provide
Description: The index is a positive integer starting from 0
// Create an empty array var Color = new new the Array (); // for array assignment Color [0] = 'Red' ; Color [ . 1] = 'Yellow' ; Color [ 2] = 'Green' ; the console.log (Color); // assignment was created, a method var the nums = new new the Array (1,3,57 ); ; the console.log (the nums) // assignment when creating, two var info = [. 6, 'Job', 99 ] the console.log (info) // value, the index console.log (info [1])
result:
["red", "yellow", "green"] [1, 3, 57] [6, "job", 99] job
Array length
Syntax: array.length
Function: Gets the length of the array array
Return value: number
Description:
1, items can be removed by setting the length from the end of the array, or add a new item to the array.
2, when a value of the current position in the array exceeds the size of the array will recalculate their length value, a length value is equal to the last index plus one.
var ARR = [ 'A', 'B', 'C', 'D' ] the console.log (arr.length); // array length arr.length = 2; // length of the array of the first two reservations, remaining deleted the console.log (ARR); ARR [ 99] = 100 ; the console.log (arr.length); // index of the last one is 99, the length of 100
result:
4 ["a", "b"] 100
Iterate
var arr = ['a','b','c','d'] for (var i=0;i<arr.length;i++){ console.log(arr[i]) }
push和unshift
push syntax:
arrayObject.push(newele1,newele2,....,neweX)
Features:
It is added to the sequence parameter arrayObject tail.
return value:
Add to a new value for the specified length of the array.
unshift syntax:
arrayObject.unshift(newele1,newele2,....,neweX)
Features:
Add it to the beginning of the order parameter of arrayObject.
return value:
Add to a new value for the specified length of the array.
//push var color = new Array('red','green'); var len = color.push('blue','yellow'); console.log(color); console.log(len); //unshift var num = [2,7,8,6]; var size = num.unshift(99,66); console.log(num)
result;
["red", "green", "blue", "yellow"] 4 [99, 66, 2, 7, 8, 6]
pop and shift
pop syntax:
arrayObject.pop()
Features:
Delete the last element of arrayObject
return value:
That element is deleted
shift syntax:
arrayObject.shift()
Features:
Remove the first item in the arrayObject
return value:
That element is deleted
//pop var color = new Array('red','green'); var n = color.pop(); console.log(n); console.log(color); //shift var num = [2,7,8,6]; var m = num.shift(); console.log(m); console.log(num);
result:
green ["red"] 2 [7, 8, 6]
join
grammar:
arrayObject.join(separator)
Features:
For all the elements in the array into a string.
Return value: String.
var nums = [2,4,5]; var numstr = nums.join(); //默认,分隔 console.log(numstr); var words = ['left','right','top']; var wordstr = words.join('-'); console.log(wordstr);
result:
2,4,5
left-right-top
reverse
grammar:
stringObject.reverse()
Features:
For the elements in the array in reverse order.
Returns: array.
var num = [2,7,8,6 ]; num.reverse (); console.log (whether);
result:
[6, 8, 7, 2]
Small Exercise:
To an array strs = [ 'a', 'b', 'c', 'd'] ;, return dcba
var strs = ['a','b','c','d']; var newstr = strs.reverse().join(''); console.log(newstr);
sort
grammar:
arrayObject.sort(sortby)
Features:
For elements of the array to be sorted.
Returns: array.
Description:
1, even if each of the array values are, Sort () method is a string comparison.
2, sort () method may receive a comparison function as an argument.
var ARR = [5,76,34,52,99, -3,56 ]; // default arr.sort () // descending arr.sort ( function (A, B) { return B- A}); / / ascending arr.sort ( function (A, B) { return A- B}); the console.log (ARR)
concat
grammar:
arrayObject.concat(arrayX,arrayX,......,arrayX)
Features:
For connecting two or more arrays.
return value:
Array.
var arr1 = ['1','43','e'], arr2 = ['a','b','c',88], arr3; arr3 = arr1.concat(arr2,['77','mm']); console.log(arr3);
result:
["1", "43", "e", "a", "b", "c", 88, "77", "mm"]
slice
grammar:
arrayObject.slice(start,end)
Features:
Returns selected elements from the existing array.
parameter:
start (Required) is selected from a predetermined start where, if it is negative, then it is from the end of the array begin to run a predetermined position.
end (optional) where the end of a predetermined selection, the index is an array of fragments at the end of the array.
Description:
1, if the end is not specified, then sliced array contains all of the elements from the start to the end of the array.
2, if there is a negative argument slice () method is used to determine the length of the array plus the number of positions corresponding to
Returns: Array
var colors = ['red','green','blue','yellow']; var newColor = colors.slice(1,3); console.log(newColor);
result:
["green", "blue"]
splice
splice arrays can delete items, increasing array of items, the replacement array items.
delete
grammar:
arrayObject.splice(index,count)
Features:
Delete zero or more elements starting from index.
return value:
An array containing the deleted elements.
Description:
count the number of items to be deleted, if set to 0, it will not remove items.
If not set, all values are deleted from the index began.
var strs = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]; var delstr strs.splice = (2,2 ); console.log (delstr); console.log (STRs);
result:
["c", "d"]
["a", "b", "e", "f", "g"]
insert:
grammar:
arrayObject.splice(index,0,item1,.....,itemX)
Features:
Value is inserted at the specified position
parameter:
Index: starting position
0: The number of items to be deleted
item1 ... itemX: items to be inserted
Returns: Array
var strs = ['a','b','c','d','e','f','g']; var insertArr = strs.splice(3,0,'mmmm','nnnn','kkkk'); console.log(insertArr); console.log(strs);
result:
[]
["a", "b", "c", "mmmm", "nnnn", "kkkk", "d", "e", "f", "g"]
replace:
grammar:
arrayObject.splice(index,count,item1,.....,itemX)
Features:
Insert value in the specified location and delete any number of items
parameter:
Index: starting position
count: the number of items to be deleted
item1 ... itemX: items to be inserted
Returns: delete the items from the original array (If you do not delete any item, empty array)
var strs = ['a','b','c','d','e','f','g']; var replacesttr = strs.splice(1,2,'XX','YY','ZZ'); console.log(replacesttr); console.log(strs);
result:
["b", "c"]
["a", "XX", "YY", "ZZ", "d", "e", "f", "g"]
indexOf
grammar:
arrayObject.indexOf(searchvalue,startIndex)
Features:
Find backwards from the beginning of the array (position 0).
parameter:
searchvalue: Required, item you are looking for;
startIndex: Optional, the starting position of the index.
return value:
number, find the location of items in the array, the case is not found, returns -1.
var nums = [2,4,6,8,89,322,7,6,21]; var num1 = nums.indexOf(6); console.log(num1); var num2 = nums.indexOf(88); console.log(num2); var num3 = nums.indexOf(6,3); console.log(num3);
result:
2 -1 7
lastIndexOf
grammar:
arrayObject.lastIndexOf(searchvalue,startIndex)
Features:
Starting from the end of the array look forward.
parameter:
searchvalue: Required, item you are looking for;
startIndex: Optional, the starting position of the index.
return value:
number, find the location of items in the array, the case is not found, returns -1.
var nums = [2,4,6,8,89,322,7,6,21]; var num1 = nums.lastIndexOf(6); console.log(num1); var num2 = nums.lastIndexOf(88); console.log(num2); var num3 = nums.lastIndexOf(6,3); console.log(num3);
result:
7 -1 2