1. Array.forEach()
將數(shù)組中的每個元素執(zhí)行傳進提供的函數(shù)搜立,沒有返回值程腹,不改變原來的數(shù)組。除非在執(zhí)行函數(shù)時對數(shù)組進行了相應操作儒拂。
例如:
var test = [1, 2, 3, 4]
test.forEach(x => {console.log(x)}) // 依次輸出1 2 3 4
console.log(test) // [1, 2, 3, 4]
var test = [1, 2, 3, 4]
test.forEach((x,y) => {test[y] = 2})
console.log(test) // [2, 2, 2, 2]
可以看出寸潦,如果forEach傳參時只傳了一個色鸳,則默認傳的是value值。若穿了(x, y)這種形式见转,則默認是(value, key)形式命雀。
2. Array.map()
該方法與forEach類似,區(qū)別在于將執(zhí)行結(jié)果組成新的數(shù)組返回斩箫,沒有改變原來的數(shù)組吏砂。與foeEach區(qū)別在于返回了一個新的數(shù)組。
var test = [1, 2, 3, 4]
test1 = test.map(x => x * 2);
console.log(test) // [1, 2, 3, 4]
console.log(test1) // [2, 4, 6, 8]
3. Array.filter()
將數(shù)組所有元素進行判斷乘客,把符合要求的元素組成新數(shù)組返回狐血。
var test = [1, 2, 3, 4]
test1 = test.filter(x => x > 2);
console.log(test) // [1, 2, 3, 4]
console.log(test1) // [3, 4]
4. Array.some()
將所有元素進行判斷,返回一個布爾值易核,如果其中有元素滿足條件則返回true匈织,若都不滿足則返回false。
var test = [1, 2, 3, 4]
test1 = test.some(x => x > 3);
console.log(test) // [1, 2, 3, 4]
console.log(test1) // true
5. Array.every()
與Array.come()類似牡直,區(qū)別在于其中所有元素都滿足才返回true缀匕,只要有一個不滿足就返回false。
var test = [1, 2, 3, 4]
test1 = test.every(x => x > 3);
console.log(test) // [1, 2, 3, 4]
console.log(test1) // false
6. Array.push()與Array.pop()
push是在數(shù)組后面添加新元素碰逸,返回最后一個添加的元素乡小;
pop是刪除最后一個元素,兩者皆改變了原數(shù)組饵史。
var test = [1, 2, 3, 4]
test1 = test.push(5, 6, 7);
console.log(test) // [1, 2, 3, 4, 5, 6, 7]
console.log(test1) // 7
var test = [1, 2, 3, 4]
test1 = test.pop();
console.log(test) // [1, 2, 3]
console.log(test1) // 4
7. Array.shift()與Array.unshift()
shift是刪除數(shù)組第一個元素满钟,并返回被刪除的元素。
unshift是在數(shù)組頭部添加相應的元素胳喷,并返回添加后數(shù)組的length零远。兩者皆改變了原數(shù)組。
var test = [1, 2, 3, 4]
test1 = test.shift();
console.log(test) // [2, 3, 4]
console.log(test1) // 1
var test = [1, 2, 3, 4]
test1 = test.unshift(-1, 0);
console.log(test) // [-1, 0, 1, 2, 3, 4]
console.log(test1) // 6
8. Array.reduce()與Array.reduceRight()
reduce是將數(shù)組所有元素都調(diào)用返回函數(shù)厌蔽,并返回最后結(jié)果牵辣,傳入的值必須是函數(shù)類型。不改變原數(shù)組奴饮。
var test = [1, 2, 3, 4]
function add(a, b) {
return a + b;
}
test1 = test.reduce(add)
console.log(test) // [1, 2, 3, 4]
console.log(test1) // 10
也可以寫成:
var test = [1, 2, 3, 4]
test1 = test.reduce((a, b) => {return a+b})
console.log(test) // [1, 2, 3, 4]
console.log(test1) // 10
reduceRight遇之類似纬向,區(qū)別是reduceRight是從右往左執(zhí)行罷了。
9. Array.isArray()
判斷對象是否是數(shù)組戴卜,返回布爾值逾条。不改變原數(shù)組。
var a = 1
var test = [1, 2, 3, 4]
console.log(Array.isArray(a)) // false
console.log(Array.isArray(test)) // true
10. Array.concat()
將多個數(shù)組拼接成一個投剥。返回拼接后的新數(shù)組师脂,不改變原數(shù)組
var test1 = [1, 2, 3, 4]
var test2 = [5, 6]
test3 = test1.concat(test2)
console.log(test3) // [1, 2, 3, 4, 5, 6]
console.log(test1) // [1, 2, 3, 4]
console.log(test2) // [5, 6]
11. Array.toString()
將數(shù)組轉(zhuǎn)化為字符串,返回字符串,不改變原數(shù)組
var test = [1, 2, 3, 4]
test1 = test.toString()
console.log(test) // [1, 2, 3, 4]
console.log(test1) // 1,2,3,4
12. Array.join()
與toString()類似吃警,區(qū)別在于可以傳入?yún)?shù)糕篇,表示每個元素之間的間隔。返回字符串酌心,不改變原數(shù)組拌消。
var test = [1, 2, 3, 4]
test1 = test.join('!')
console.log(test) // [1, 2, 3, 4]
console.log(test1) // 1!2!3!4
13. Array.splice()
很靈活的方法,具體使用方法為:Array.splice(num1, num2, arg1, arg2...)
num1為操作執(zhí)行開始的位置安券,如0表示從array[0]墩崩,即數(shù)組頭部開始。
num2表示要刪除的元素個數(shù)侯勉,從num1開始執(zhí)行(包括num1)
arg1,arg2.....表示要添加的元素鹦筹。
該方法返回刪除的元素組成的數(shù)組,不會改變原數(shù)組址貌。
var test = [1, 2, 3, 4]
test1 = test.splice(1, 2, 'haha!', 'wow!')
console.log(test) // [1, 'haha', 'wow', 4]
console.log(test1) // [2, 3]
14. Array.slice()
使用方法為Array.slice(start, end)铐拐,表示從已有的數(shù)組中返回選定的元素。
返回選定元素組成的數(shù)組芳誓,該方法不改變原數(shù)組余舶。
注意(start, end)這里被處理成[1, 2)啊鸭,即包括start锹淌,但不包括end。
var test = [1, 2, 3, 4]
test1 = test.slice(0, 1)
console.log(test) // [1, 2, 3, 4]
console.log(test1) // [1]
若只傳入一個值赠制,則表示從該值到數(shù)組末尾赂摆。
var test = [1, 2, 3, 4]
test1 = test.slice(1)
console.log(test) // [1, 2, 3, 4]
console.log(test1) // [2, 3, 4]