# 爱上Javascript数组Array（二）——方法解读

## array.concat()

``array.concat(value1, value2, ..., valueN) ``

``var arr1 = [1, 2, 3]; var obj = {animal : 'monkey'}; var arr2 = arr1.concat([4, 5, 6], obj, [7, 8, 9]); // arr1 [1, 2, 3] // arr2 [1, 2, 3, 4, 5, 6, {animal : 'monkey'}, 7, 8, 9]  obj.animal = 'tiger'; // [1, 2, 3, 4, 5, 6, {animal : 'tiger'}, 7, 8, 9] ``

## array.join()

``array.join(seperator) ``

``var arr1 = [1, 2, 3]; var str = arr1.join(); // 1,2,3 str = arr1.join('#'); // 1#2#3 ``

``var str = new Array(3).join('-+'); // -+-+ ``

``var arr1 = [1, 2, 3]; arr1.toString(); // 1,2,3 ``

## array.push()

``array.push(value1, value2, ..., valueN); ``

``var arr1 = [1, 2, 3]; var len = arr1.push(4, 5);  console.log(len); // 5 console.log(arr1); // [1, 2, 3, 4, 5] ``

``arr1[arr1.length] = 6; // [1, 2, 3, 4, 5, 6]    ``

## array.pop()

``var arr1 = [1, 2, 3]; arr.pop(); // [1, 2] 返回 3 ``

## array.unshift()

``var arr1 = [1, 2, 3]; var len = arr1.unshift(4, 5);  console.log(len); // 5 console.log(arr1); // [4, 5, 1, 2, 3] ``

## array.shift()

``var arr1 = [1, 2, 3]; arr.shift(); // [2, 3] 返回 1 ``

## array.sort()

``var arr1 = [1, 2, 3, 14, 24]; arr1.sort(); // [1, 14, 2, 24, 3] ``

``var compare = function(a, b){     return a - b; }  var arr2 = [1, 12, 2, 23, 3 , 5, 4]; arr2.sort(compare); // [1, 2, 3, 4, 5, 12, 23] ``

``var arr3 = ['F', 'e', 'f', 'E']; arr3.sort(function(a, b){     return a.localeCompare(b); }); // ['e', 'E', 'f', 'F']  ``

## array.reverse()

``var arr1 = [1, 4, 3, 2]; arr1.reverse(); // [2, 3, 4, 1] ``

## array.slice()

``array.slice(start, end); ``

``var arr1 = [1, 2, 3, 4, 5, 6];  arr1.slice(4); // [5, 6] arr1.slice(2, 4); // [3, 4] arr1.slice(-3); // [4, 5, 6] ``

## array.splice()

``array.slice(start, count, item); ``

``var arr1 = [1, 2, 3, 4, 5]; //删除 arr1.splice(2, 1); // [1, 2, 4, 5]  返回 [3] //插入 arr1.splice(3, 0, 6, 7); // [1, 2, 4, 6, 7, 5] //替换 arr1.splice(1, 2, 8, 9); // [1, 8, 9, 6, 7, 5]  返回[2, 4] ``

## indexOf() 与 lastIndexOf()

``var arr1 = [1, 2, 3, 4, 3, 2, 1]; arr1.indexOf(2); // 1 arr1.indexOf(2, 3); // 5  arr1.lastIndexOf(3); // 4 arr1.lastIndexOf(3, 4) // 2 ``

## array.every()

``var arr1 = [1, 2, 3, 4, 5]; arr1.every(function(item, index, array){     return item > 3; }); // false ``

## array.some()

``arr1.some(function(item, index, array){     return item > 3; }); // true ``

## array.map()

``arr1.map(function(item, index, array){     return item * 2; }); // [2, 4, 6, 8, 10] ``

## array.filter()

``arr1.filter(function(item, index, array){     return item > 3; }); // [4, 5] ``

## array.reduce() 与 array.reduceRight()

``var splitstr = function(prev, item, index, array){     return prev + '#' + item; }  var arr1 = [1, 2, 3, 4, 5]; arr1.reduce(splitstr, 8); // 8#1#2#3#4#5 arr1.reduceRight(splitstr, 8); // 8#5#4#3#2#1 ``