Array creation
There are two ways to create arrays in JavaScript, the first of which is to use the array constructor:
?
123 |
var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20); // 创建一个包含20项的数组 var arr3 = new Array( "lily" , "lucy" , "Tom" ); // 创建一个包含3个字符串的数组 |
The second basic way to create an array is by using the array literal notation:
?
123 |
var arr4 = []; //创建一个空数组 var arr5 = [20]; // 创建一个包含1项的数组 var arr6 = [ "lily" , "lucy" , "Tom" ]; // 创建一个包含3个字符串的数组 |
When reading and setting the values of an array, use square brackets and provide a 0-based numeric index of the corresponding values:
?
1234 |
var arr6 = [ "lily" , "lucy" , "Tom" ]; // 创建一个包含3个字符串的数组 alert(arr6[0]); //lily arr6[1] = "mary" ; //修改第二项为mary arr6[3] = "sean" ; //增加第四项为sean |
The length property of an array in JavaScript can be modified, as shown in the following example:
?
123 |
var arr = [ "lily" , "lucy" , "Tom" ]; // 创建一个包含3个字符串的数组 arr[arr.length] = "sean" ; //在下标为3处(也就是数组尾部)添加一项"sean" arr.length = arr.length-1; //将数组的最后一项删除 |
If you need to determine if an object is not an array object, before ECMAScript 5, we can judge by instanceof array, but the problem with the instanceof operator is that it assumes that there is only one global execution environment. If a page contains more than one frame, there are actually more than two different global execution environments, and there are more than two different versions of the Array constructor. If you pass an array from one frame to another, the incoming array has a different constructor than the array that was created natively in the second frame.
ECMAScript 5 adds the Array.isarray () method. The purpose of this method is to ultimately determine whether a value is an array, regardless of which global execution environment it was created in.
Array method
The following is the beginning of the method of the array, the array method has the array prototype method, also has the method which inherits from the object objects, here we only introduce the array prototype method, the array prototype method mainly has the following:
Join ()
Push () and pop ()
Shift () and unshift ()
Sort ()
Reverse ()
Concat ()
Slice ()
Splice ()
IndexOf () and LastIndexOf () (ES5 new)
ForEach () (ES5 new)
Map () (ES5 new)
Filter () (ES5 new)
Every () (ES5 new)
Some () (ES5 new)
Reduce () and Reduceright () (ES5 new)
New method Browser support for ES5:
Opera 11+
Firefox 3.6+
Safari 5+
Chrome 8+
Internet Explorer +
For supported browser versions, this can be done through the array prototype extension. The basic functions of each method are described in detail below.
console.log(arr.join());
// 1,2,3
console.log(arr.join(
"-"
));
// 1-2-3
console.log(arr);
// [1, 2, 3](原数组不变)
By using the Join () method, you can implement a repeating string, simply passing in the string and repeating the number of times, to return the duplicate string, the function is as follows:
?
12345 |
function repeatstring (str, n) { return new array (n + 1). Join (str); } console.log (repeatstring ( "ABC" //abcabcabc console.log (repeatstring ( Code class= "JS string" > "Hi" //Hihihihihi |
var
count = arr.push(
"Jack"
,
"Sean"
);
console.log(count);
// 5
console.log(arr);
// ["Lily", "lucy", "Tom", "Jack", "Sean"]
var
item = arr.pop();
console.log(item);
// Sean
console.log(arr);
// ["Lily", "lucy", "Tom", "Jack"]
var
count = arr.unshift(
"Jack"
,
"Sean"
);
console.log(count);
// 5
console.log(arr);
//["Jack", "Sean", "Lily", "lucy", "Tom"]
var
item = arr.shift();
console.log(item);
// Jack
console.log(arr);
// ["Sean", "Lily", "lucy", "Tom"]
console.log(arr1.sort());
// ["a", "b", "c", "d"]
arr2 = [13, 24, 51, 3];
console.log(arr2.sort());
// [13, 24, 3, 51]
console.log(arr2);
// [13, 24, 3, 51](元数组被改变)
To solve the above problem, the sort () method can receive a comparison function as a parameter so that we specify which value is in front of which value. The comparison function receives two parameters, returns a negative number if the first argument should precede the second, and returns 0 if the two arguments are equal, or returns a positive number if the first argument should be in the second one. Here is a simple comparison function:
?
1234567891011 |
function compare (value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } ARR2 = [3]; console.log (arr2.sort (Compare)); //[3, in.] |
If you need to produce a descending sort result by comparing functions, simply swap the values returned by the comparison function:
?
1234567891011 |
function compare (value1, value2) { if (value1 < value2) { return 1; } else if (value1 > value2) { return -1; } else { return 0; } ARR2 = [3]; console.log (arr2.sort (Compare)); //[Wuyi, 3] |
console.log(arr.reverse());
//[3, 51, 24, 13]
console.log(arr);
//[3, 51, 24, 13](原数组改变)
var
arrCopy = arr.concat(9,[11,13]);
console.log(arrCopy);
//[1, 3, 5, 7, 9, 11, 13]
console.log(arr);
// [1, 3, 5, 7](原数组未被修改)
From the above test results can be found: the incoming is not an array, the parameter is added directly after the array, if the array is passed in, the array of the individual items are added to the array. But what if a two-dimensional array is passed in?
?
123 |
var arrCopy2 = arr.concat([9,[11,13]]); console.log(arrCopy2); //[1, 3, 5, 7, 9, Array[2]] console.log(arrCopy2[5]); //[11, 13] |
In the code above, the fifth item of the ARRCOPY2 array is an array of two items, that is, the Concat method can only add each item in the array to the arrays, and if some of the entries in the array are arrays, the array entry is also added to the ArrCopy2 as an item.
var
arrCopy = arr.slice(1);
var
arrCopy2 = arr.slice(1,4);
var
arrCopy3 = arr.slice(1,-2);
var
arrCopy4 = arr.slice(-4,-1);
console.log(arr);
//[1, 3, 5, 7, 9, 11](原数组没变)
console.log(arrCopy);
//[3, 5, 7, 9, 11]
console.log(arrCopy2);
//[3, 5, 7]
console.log(arrCopy3);
//[3, 5, 7]
console.log(arrCopy4);
//[5, 7, 9]
Arrcopy only set a parameter, that is, the starting subscript is 1, so the returned array is subscript 1 (including subscript 1) to start to the end of the array.
The arrCopy2 sets two parameters, returning a sub-array starting with the subscript (including 1) to the terminating subscript (excluding 4).
ArrCopy3 sets two parameters, terminates subscript negative, and, when negative, replaces a negative number with the value of the array length (6) to replace that position, so it is a sub-array starting from 1 to 4 (not included).
Both parameters in ArrCopy4 are negative, so the array length of 6 is converted to positive numbers, so it is equivalent to slice (2,5).
var
arrRemoved = arr.splice(0,2);
console.log(arr);
//[5, 7, 9, 11]
console.log(arrRemoved);
//[1, 3]
var
arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr);
// [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2);
// []
var
arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr);
// [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3);
//[7]
console.log(arr.indexOf(5));
//2
console.log(arr.lastIndexOf(5));
//5
console.log(arr.indexOf(5,2));
//2
console.log(arr.lastIndexOf(5,4));
//2
console.log(arr.indexOf(
"5"
));
//-1
arr.forEach(
function
(x, index, a){
console.log(x +
‘|‘
+ index +
‘|‘
+ (a === arr));
});
// 输出为:
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true
var
arr2 = arr.map(
function
(item){
return
item*item;
});
console.log(arr2);
//[1, 4, 9, 16, 25]
var
arr2 = arr.filter(
function
(x, index) {
return
index % 3 === 0 || x >= 8;
});
console.log(arr2);
//[1, 4, 7, 8, 9, 10]
var
arr2 = arr.every(
function
(x) {
return
x < 10;
});
console.log(arr2);
//true
var
arr3 = arr.every(
function
(x) {
return
x < 3;
});
console.log(arr3);
// false
var
arr2 = arr.some(
function
(x) {
return
x < 3;
});
console.log(arr2);
//true
var
arr3 = arr.some(
function
(x) {
return
x < 1;
});
console.log(arr3);
// false
var
sum = values.reduceRight(
function
(prev, cur, index, array){
return
prev + cur;
},10);
console.log(sum);
//25
The above is a small series to introduce you to the JavaScript array method Daquan (recommended), I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you.
JavaScript Array method Daquan (recommended for Gray)