[TOC]
# **JavaScript常用數組操作方法_ES5**
## **一、concat()**
>[info]concat() 方法用于連接兩個或多個數組。該方法不會改變現有的數組,僅會返回被連接數組的一個副本。
~~~
var arr1 = [1,2,3];
var arr2 = [4,5];
var arr3 = arr1.concat(arr2);
console.log(arr1); //[1, 2, 3]
console.log(arr3); //[1, 2, 3, 4, 5]
~~~
## **二、join()**
>[info]join() 方法用于把數組中的所有元素放入一個字符串。元素是通過指定的分隔符進行分隔的,默認使用','號分割,不改變原數組。
~~~
var arr = [2,3,4];
console.log(arr.join()); //2,3,4
console.log(arr); //[2, 3, 4]
~~~
## **三、push()**
>[info]push() 方法可向數組的末尾添加一個或多個元素,并返回新的長度。末尾添加,返回的是長度,會改變原數組。
~~~
var a = [2,3,4];
var b = a.push(5);
console.log(a); //[2,3,4,5]
console.log(b); //4
push方法可以一次添加多個元素push(data1,data2....)
~~~
## **四、pop()**
>[info]pop() 方法用于刪除并返回數組的最后一個元素。返回最后一個元素,會改變原數組。
~~~
var arr = [2,3,4];
console.log(arr.pop()); //4
console.log(arr); //[2,3]
~~~
## **五、shift()**
>[info]shift() 方法用于把數組的第一個元素從其中刪除,并返回第一個元素的值。返回第一個元素,改變原數組。
~~~
var arr = [2,3,4];
console.log(arr.shift()); //2
console.log(arr); //[3,4]
~~~
## **六、unshift()**
>[info]unshift() 方法可向數組的開頭添加一個或更多元素,并返回新的長度。返回新長度,改變原數組。
~~~
var arr = [2,3,4,5];
console.log(arr.unshift(3,6)); //6
console.log(arr); //[3, 6, 2, 3, 4, 5]
tip:該方法可以不傳參數,不傳參數就是不增加元素。
~~~
## **七、slice()**
>[info]返回一個新的數組,包含從 start 到 end (不包括該元素)的 arrayObject 中的元素。返回選定的元素,該方法不會修改原數組。
~~~
var arr = [2,3,4,5];
console.log(arr.slice(1,3)); //[3,4]
console.log(arr); //[2,3,4,5]
~~~
## **八、splice()**
>[info]splice() 方法可刪除從 index 處開始的零個或多個元素,并且用參數列表中聲明的一個或多個值來替換那些被刪除的元素。如果從 arrayObject 中刪除了元素,則返回的是含有被刪除的元素的數組。splice() 方法會直接對數組進行修改。
~~~
var a = [5,6,7,8];
console.log(a.splice(1,0,9)); //[]
console.log(a); // [5, 9, 6, 7, 8]
var b = [5,6,7,8];
console.log(b.splice(1,2,3)); //[6, 7]
console.log(b); //[5, 3, 8]
~~~
## **九、substring() 和 substr()**
>[info]相同點:如果只是寫一個參數,兩者的作用都一樣:都是是截取字符串從當前下標以后直到字符串最后的字符串片段。
substr(startIndex);
substring(startIndex);
~~~
var str = '123456789';
console.log(str.substr(2)); // "3456789"
console.log(str.substring(2)) ;// "3456789"
~~~
>不同點:第二個參數
substr(startIndex,lenth): 第二個參數是截取字符串的長度(從起始點截取某個長度的字符串);
substring(startIndex, endIndex): 第二個參數是截取字符串最終的下標 (截取2個位置之間的字符串,‘含頭不含尾’)。
~~~
console.log("123456789".substr(2,5)); // "34567"
console.log("123456789".substring(2,5)) ;// "345"
~~~
## **十、sort 排序**
>[info]按照 Unicode code 位置排序,默認升序
~~~
var fruit = ['cherries', 'apples', 'bananas'];
fruit.sort(); // ['apples', 'bananas', 'cherries']
var scores = [1, 10, 21, 2];
scores.sort(); // [1, 10, 2, 21]
~~~
## **十一、reverse()**
>[info]reverse() 方法用于顛倒數組中元素的順序。返回的是顛倒后的數組,會改變原數組。
~~~
var arr = [2,3,4];
console.log(arr.reverse()); //[4, 3, 2]
console.log(arr); //[4, 3, 2]
~~~
## **十二、indexOf 和 lastIndexOf**
>[info]都接受兩個參數:查找的值、查找起始位置
不存在,返回 -1 ;存在,返回位置。indexOf 是從前往后查找, lastIndexOf 是從后往前查找。
`indexOf`
~~~
var a = [2, 9, 9];
a.indexOf(2); // 0
a.indexOf(7); // -1
if (a.indexOf(7) === -1) {
// element doesn't exist in array
}
~~~
`lastIndexOf`
~~~
var numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3
~~~
## **十三、every**
>[info]對數組的每一項都運行給定的函數,每一項都返回 ture,則返回 true
~~~
function isBigEnough(element, index, array) {
return element < 10;
}
[2, 5, 8, 3, 4].every(isBigEnough); // true
~~~
## **十四、some**
>[info]對數組的每一項都運行給定的函數,任意一項都返回 ture,則返回 true
~~~
function compare(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(compare); // false
[12, 5, 8, 1, 4].some(compare); // true
~~~
## **十五、filter**
>[info]對數組的每一項都運行給定的函數,返回 結果為 ture 的項組成的數組
~~~
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];
var longWords = words.filter(function(word){
return word.length > 6;
});
// Filtered array longWords is ["exuberant", "destruction", "present"]
~~~
## **十六、map**
>對數組的每一項都運行給定的函數,返回每次函數調用的結果組成一個新數組
~~~
var numbers = [1, 5, 10, 15];
var doubles = numbers.map(function(x) {
return x * 2;
});
// doubles is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]
~~~
## **十七、forEach 數組遍歷**
~~~
const items = ['item1', 'item2', 'item3'];
const copy = [];
items.forEach(function(item){
copy.push(item)
});
~~~