Javascript array concept and common methods Summary

First, what is a JavaScript array

JavaScript array is an ordered collection of values, each value is called an element or element, and each element has a location in the array, expressed as a number, called an index or subscript. js untyped array, the array elements can be of any type, with an array of different elements may be an array or an object, regardless of the order can be repeated. The index of the array elements need not be contiguous, there may be gaps. Each array has a property lengrh, length is the number of array elements properties.

Second, the way to create an array of JavaScript

1, literal creation
var arr=[1,3,8,6,9];
var arr2=["a","b","c"];
2, constructor creates
var  arr=new Array(4,5,3,6,2,,1);
var  arr1=new Array(2,1,3,8,5,4,6);

Note: If you create an array by array constructor, only the parameter a, and this parameter is a positive integer, then a new comprising empty array of the integer length, which has the plurality of empty elements length; if it is negative or fractional when being given; if the value is not entered, when other types, the data will be 0 as the first element of the array, length. 1
var the array of arr1 new new = (. 5);
the console.log (of arr1); // length 5 is empty array
var arr2 is the array = new new (3.5 of);
the console.log (arr2 is); // given
var ARR3 the array = new new ( "a");
the console.log (ARR3 [0]); // "a"
added: wherein is the maximum length of index (index) number, the array element is an array of length - 1 (the number of elements -1);
var of arr1 = [3,5,2,3,5,2];
var = arr2 is [3,5,2,3,5,2];
the console.log (of arr1 [. 1]); //. 5
th @ 8 is the length of the array, but also elements; console.log (arr1.length) number
of arr1 [. 4] = 2;
arr2 is [-arr.length. 1] = 2;
arr2 is [arr.length] = 20 is; // Add to a most end of the array element 20 is
the console.log (arr2 is); // [. 3 , 5,2,3,5,2,20]
var ARR = [1,2,3,4,5];
arr2.length =. 3; // array length It can be modified
console.log (arr2); // [1,2,3 ] array becomes 3 i.e. elements
arr1.length-; // array length minus one
console.log (arr1); // [3,5,2 , 3,5] i.e., the length of the array is reduced by one
arr1.length = 0; // clear the entire array
console.log (arr1); // [] empty element becomes empty array

Third, the basic method of the array

1, push () method

push () at the end of the array to add one or more elements, and returns the new length of the array
var arr = [1,2,3,4,5,6,7,8,9,10,11];
Usage: arr. Push ( "a", "B");
arr.push (. 11); // insert a new element at the end of the array
arr.push (11,12,25); // end of the array is inserted in a plurality of new elements
var a = arr.push (6,7,8); // returned by addition to an array of new elements of the tail length
console.log (arr); // [1,2,3,4,5,6,7 , 8,9,10,11,11,11,12,25,6,7,8] 18

2, pop () method

pop () to remove the last element of the array of the tail, and will return the removed element
Usage: arr.pop (); // pop no parameters, an array of the most deleting a trailing element
var a = arr.pop (); // pop a tail remove most of the array element, and returns the removed element
var ARR = [1,2,3,4,5,6,7,8,9,10,11];
var a = ARR. POP ();
the console.log (ARR, A); // [1,2,3,4,5,6,7,8,9,10]. 11
supplement:
// arr.length-; // equivalent speed faster, but it will not return the removed element
// arr.pop (); // slow, but will return the element to be deleted

3, toString () method

toString () array into a string:
var ARR = [. 1, 2,. 3,. 4,. 5];
var = arr.toString STR ()
the console.log (STR) // 1,2,3,4,5

4, join () method

join () is each element of the array is connected to the designated character string is returned form a new
array combined return a string, default connection
Usage: var str = arr.join ()
where the argument is a string join the connector
// var = arr.join STR ( "|");
"" as a connector, the array elements will be joined end to end string
var ARR = [1,2,3,4,5];
var STR = arr.join ( "|")
the console.log (STR); //. 1 | 2 |. 3 |. 4 |. 5
var str1 arr.join = ( "");
the console.log (str1); // 12345

5, shift () method

shift () removes the first element of the array, and returns the removed element
Usage: arr.shift (); // the first element of the array delete
var arr = [1,2,3,4,5];
var a = arr.shift (); // the first element of the array delete, and return the removed element
console.log (arr, a); // [2,3,4,5] 1

6, unshift () method

the unshift () to add one or more elements in the array head, and returns the new length of the array
Usage: arr.unshift (0); // in the head insert a new element array
//arr.unshift(-3); // insert an element at the head of the array
// arr.unshift (-3, -2, -1,0 ); // new element inserted into a plurality of head array
var arr = [1,2,3, 4,5];
var arr.unshift A = (-3, -2);
the console.log (ARR, A); // [-. 3, -2,1,2,3,4,5]. 7

7, concat () method

concat () array is merged, a new array will return later, the two arrays does not change the original
Usage:
1) combining two arrays
var = arr2 is arr.concat (of arr1);
2) in addition to an array of arrays combined, is also elements can be combined, a plurality of combined elements of the original array, and returns the new array
var arr.concat ARR3 = (l, 2,3);
. 3) may be the concat consolidated element may merge array
var arr4 = arr.concat (0 , of arr1, [ "a", "B", "C"]);
. 4) if the direct use concat, the array can be completed replication
var arr5 arr.concat = ();
var ARR = [l, 2,3 , 4,5];
var of arr1 = [3,4, 5];
var = arr2 is arr.concat (of arr1);
the console.log (arr2 is); // [1,2,3,4,5,3,4 ,. 5]
var = arr.concat ARR3 (l, 2,3);
the console.log (ARR3); // [1,2,3,4,5,1,2,3]
var arr4 arr.concat = ( 0, of arr1, [ "A", "B", "C"]);
the console.log (arr4); // [1,2,3,4,5,3,4,5,0, "A" , "B", "C" ]

8, sort () method

sort () is used to sort the elements of the array
when the sort, sort () method is invoked for each item in the array toString () transformation methods, and the resulting string comparison, to determine how to sort. Even if the tuple is a value, Sort () method is to compare strings, so there will be the following case:
var ARR = [ "A", "D", "C", "B"] ;
the console.log (arr.sort ()); // [ "A", "B", "C", "D"]
of arr1 = [10, 15, 51 is,. 9];
arr1.sort ()
Console. log (arr1); // [10 , 15, 51, 9]
from the above code is not seen on the digital values sorted by size, are sorted in order of character encoding, to achieve this, it is necessary to use a sort function:
var ARR = [1,2,6,3,8,4,5];
arr.sort (function (the latter, before a) {})
arr.sort (function (A, B) {
// return ab ascending
// return ba descending
return ab; // ascending
})
the console.log (ARR); // [1,2,3,4,5,6,8]
this Numerical method is for
when the element is a string:
var ARR = [ "D", "E", "B", "A", "C", "H", "J", "I", "K "];
// arr.sort ();
arr.sort (function (A, B) {
// return a.charCodeAt (0) -b.charCodeAt (0); // ascending
return b.charCodeAt (0) -a.charCodeAt (0 ); // descending
})
the console.log (ARR); // [ "K", "J", "I", "H", "E", "D", "C", "B", "A "]

9, slice () method

slice () interception of a specified location to copy the contents of the array, the array does not change the original
usage: arr.slice (from what position (subscript) and ends before what position)
Note: Only the front interception back, second parameters do not write, to intercept the tail default, when two parameters are empty array or a digital copying is 0, two parameters are copied to an empty array 0, only the intercept represents the value from a value copied to tail, is a value taken from a starting position when the negative reciprocal
var ARR = [1,2,3,4,5,6,7];
var arr.slice of arr1 = (l, 4);
the console.log (of arr1); // [2,3,4]
// = arr2 is arr.slice var (); // copy array
// var arr2 = arr.slice (0) ; // copy array
// var arr2 = arr.slice (0 , 0); // copy an empty array
var arr3 = arr.slice (3); // taken from the tail of the third copy to
the console.log (ARR3) // [4,5,6,7]
var = arr4 arr.slice (-2); // taken from the beginning to the end of the penultimate position replicate
the console.log (arr4) //[6.7]
var arr5 arr.slice = (-3, -1); // the penultimate three to the penultimate one copy
console.log (arr5); // [5,6 ]

10, splice () method

The new array splice () This method can be removed from the location specified by a given number of elements, and insert an element required in this position, and returns the removed element consisting of
Usage: arr.splice (starting from what position, how many deleted element, the element to insert);
Note:
1) the absence of any argument, returns an empty array
2) the first parameter is 0, indicating the start from No. 0, the second parameter to delete the number does not fill, which means delete to the end, is about to transfer all data to the new array, to achieve functional copy of the array.
3) From the first of several start can be negative number from back to front (countdown), because did not give the number to be deleted, so removing the tail
4) starting from the 0th bit of the array, delete an element, and in this -1 position of the insertion element, replacing
5) start the second array is replaced with a plurality of elements
6) is inserted in a second element of the array -1
var ARR = [1,2,3,4,5];
var arr1 = arr.splice (); // no how parameters
console.log (arr1); // return an empty array
var arr2 = arr.splice (0); // transfer all the data to the new array, array replicate
console. log (arr2 is); // [1,2,3,4,5]
var arr.splice ARR3 = (0,3); // start removing bits from the first three elements 0, return to the new array
console.log ( ARR3); // [l, 2,3]
var arr4 arr4.splice = (-2); // start from a few can be negative, the number of number (reciprocal) from back to front, because there is to be deleted , so removing the tail
the console.log (arr4); // [4,5]
var arr5 arr.splice = (0,1, -1); // start with the first bit of the array 0, delete an element in this position and insert a -1 elements, replacing
the console.log (arr5); // [. 1]
arr.splice (-1,1,0); // last replacement bit array 0
the console.log (ARR); // [. 1 , 2,. 3,. 4, 0]
arr.splice (2,2,1,2); // start the second array is replaced with two elements 1,2
the console.log (ARR); // [. 1 , 2,. 1, 2,. 5]
arr.splice (2,0, -1, -2, -3, -4); // bit insertion in a second array element -1, -2, -3, -4
the console.log (ARR); // [. 1, 2, -1, -2, -3, -4,. 3,. 4,. 5]

11, indexOf () method

indexOf () to start looking backwards from the beginning of the array (position 0).
Usage: indexOf (element too want to query, the query from the start what position) position is the subscript
v
var arr = [1,2,3,4,5,6]
var index = arr.indexOf (4); // array 4 in the front to rear find elements, if found, return the index element is located, if not found, it returns -1
console.log (index); // returns subscript. 3
var index1, arr.indexOf = (. 9 );
console.log (index1); // - 1
Note: when the elements in the array as an object, different addresses, can not find

12, lastIndexOf () method

lastIndexOf () starts from the end of the array lookahead
Usage: lastIndexOf (Find available elements, where you start to find)
var ARR = [1,3,1,2,3,5,2,3,4,6];
var index = arr.lastIndexOf (3); // find elements from the back in the array 3, if found, return the index element is located, if not found, it returns -1
console.log (index) ; // returns subscript. 7
var = index1, arr.lastIndexOf (100);
the console.log (index1,); // -. 1

13, forEach () method

forEach () loop to traverse the array, each array running a given function. This method has no return value. Type function parameters are the default parameters have passed, the parameters are: an array of content traverse; corresponding to the index of the first array, the array itself.
Usage:
arr.forEach (function (element array, each element corresponding to the index, the array itself) {
})
sum:
var ARR = [1,2,3,5,6,7,8,9] ;
var SUM = 0;
arr.forEach (function (Item) {// Item presentation element
SUM + = Item;
})
// 41 is; the console.log (SUM)
Note: use forEach empty element can skip
the printing element // standard, excluding empty elements
var ARR = [1,2,3,5,6,7,8,9];
arr.forEach (function (Item, index) {// Item, and the subscript index represent elements
console. log (item, index); // print element and subscripts i.e., not print empty element
})
// copy the array, excluding the null factors
var ARR = [2,4,6,8,3,2];
var of arr1 = [];
arr.forEach (function (Item, index) {
of arr1 [index] = Item;
})
console.log(arr1); //[2,4,6,8,3,2]

14, map () method

map () of the array through the loop, this method has a return value, a new array is returned to the original array of length equal
Usage:
arr.map (function (element array, each element corresponding to the index, the array itself) {
})
returns a new array
var ARR = [3,5,7,9,1,2,4];
var = arr2 is arr.map (function (Item) {// Item element represents
// return used in the map is added to the corresponding index data corresponding
return item + 10; // the return value of the item
});
the console.log (arr2 is); // [13,1,17,19,11,12,14]

15, some () method

some () to find out whether there are elements in the array to meet the conditions, if it returns true, and false if not
at the same time through the array, if there is an element that meets the conditions will return true directly, do not continue to traverse backwards
usage:
arr. some (function (element array, each element corresponding to the index, the array itself) {
})
var ARR = [1,4,6,9,0];
var = BOOL arr.some (function (Item, index , ARR) {
return Item>. 5;
});
the console.log (BOOL); // to true

16, every () method

every () returns an array ture when determining whether each condition is satisfied, if the conditions are not met, directly out, otherwise all meet
usage: arr.every (function (element in the array, each element corresponding index, the array itself) {
})
var ARR = [1,1,4,6,2,7];
var = BOOL arr.every (function (Item, index, ARR) {
return Item> 2;
});
console.log (bool); // false

17, filter () method

filter () is the determination of all elements, satisfying the condition as a new element in the array returned
Usage: arr.filter (function (element array, each element corresponding to the index, the array itself) {
return condition
}) ;
case: requirement is greater than 5 returns an array into a new array of
first thought map, but can only achieve the original map and the new array the same length as
a result of satisfying the condition screening array
var arr = [1,1,4,6 , 2,7];
var = arr.filter of arr1 (function (Item, index, ARR) {
return Item>. 5;
});
the console.log (of arr1); // [67]

18, reduce () method

reduce () function call returns all the elements, the final result of the return value, the function value must be passed Type: bit 1 from traversing the array, bit 0 is not traversed, the index starts. Where: beginning value are 0 array, behind each value is undefined, if used in the function will return the return value to the value in the next traversing the array of
arrays .reduce (callback function (cumulative values, elements , index, array) {}, the initial value)
1), if reduce no initial value, the accumulated value of the 0 element in the array, traversing from the subscript 1 start
2), if reduce sets the initial value, the accumulated value starting from this initial value is the traversal starts from index 0
usage:
arr.reduce (function (element array, each element corresponding to the index, the array itself) {
})
to accumulate the sum of array elements
// Note : reduce returns a value that is traversed to the last return out of the value of
var ARR = [10,3,4,7,3,5,8,9];
var arr.reduce = SUM (function (value, Item ) {
return value + Item;
});
the console.log (SUM); // 49
sums the premise of the base 100, 100 is the initial value of
var arr = [10,3,4,7,3,5, 8, 9 are];
var arr.reduce = SUM (function (value, Item) {
return value + Item;
}, 100);
console.log(sum); //149

19, Array.isArray () method

Array.isArray () determines whether an object is an array, returns a Boolean value, if true otherwise returns an array to false
var ARR = [1,2,3,4];
var {A obj =:. 1};
var A . 1 =;
the console.log (Array.isArray (ARR)); // to true
the console.log (Array.isArray (obj)); // to false
the console.log (Array.isArray (A)); // to false

Released seven original articles · won praise 2 · Views 113

Guess you like

Origin blog.csdn.net/weixin_44208755/article/details/105176008
Recommended