詳情請(qǐng)參考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
js中數(shù)組map
返回一個(gè)新數(shù)組炬搭,其結(jié)果是在調(diào)用數(shù)組中的每個(gè)元素上,調(diào)用提供的函數(shù):如下
var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);/*返回[2, 8, 18, 32]*/
const map2 = array1.map(function(x){
return x*2;
});
console.log(map2);/*返回[2, 8, 18, 32]*/
語(yǔ)法如下
array.map(function callback(currentValue,index,arr), thisValue)
參數(shù)描述
currentValue 必選穆桂。循環(huán)遍歷過(guò)程中宫盔,每個(gè)元素的值
index 可選。當(dāng)前元素的索引值
arr 可選享完。就是對(duì)當(dāng)前操作的數(shù)組灼芭;
thisValue 可選。就是指的this般又;如果不寫該值彼绷,則在callback中的this指向的時(shí)window巍佑,如果寫了則this指向你寫的值;
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組filter
其實(shí)就是一個(gè)篩選函數(shù)寄悯,并把篩選函數(shù)的結(jié)果返回到一個(gè)新的數(shù)組
var array1 = [1, 4, 9, 16];
const map3 = array1.filter(x => x>10);
console.log(map3);
const map4 = array1.filter(function(x){
return x >10;
});
console.log(map4);
語(yǔ)法
array.filter(function(currentValue,index,arr), thisValue)
參數(shù)請(qǐng)參考上文的map
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組sort
一個(gè)排序函數(shù)萤衰;單由于取決于實(shí)現(xiàn),因此無(wú)法保證排序的時(shí)間和空間復(fù)雜性
var array1 = [1, 4, 9, 16];
const map5 = array1.sort((a,b) => a>b?-1:1);
console.log(map5);
const map6 = array1.sort(function(a,b){
return a>b?-1:1
});
console.log(map6);
語(yǔ)法
array.sort(sortfunction)
參數(shù)描述
sortfunction 可選猜旬,不加入該參數(shù)時(shí)默認(rèn)Unicode代碼點(diǎn)值進(jìn)行排序脆栋。加入時(shí)必須是函數(shù)
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組reduce
把元素的每一個(gè)值進(jìn)行預(yù)算,并返回洒擦;
var array1 = [1, 4, 9, 16];
var map7 = array1.reduce(function(toal,num){
return toal + num;
});
console.log(map7);/*返回30*/
語(yǔ)法
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
參數(shù)描述
total 必需椿争。累加器,記錄每次計(jì)算后返回的結(jié)果秘遏。
currentValue 必需丘薛。循環(huán)遍歷過(guò)程中,每個(gè)元素的值
currentIndex 可選邦危。當(dāng)前元素的索引
arr 可選。就是對(duì)當(dāng)前操作的數(shù)組舍扰;
initialValue 可選倦蚪。給toal賦初始值
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組some
傳入一個(gè)函數(shù),只要數(shù)組滿足其中一個(gè)要求就返回true边苹,否則就返回false
var array1 = [1, 4, 9, 16];
var map8 = array1.some(function(e){
return e > 10
})
console.log(map8); /*返回true*/
語(yǔ)法
array.some(function(currentValue,index,arr),thisValue)
參數(shù)請(qǐng)參考上文的map
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組every
傳入一個(gè)函數(shù)陵且,只要數(shù)組所有元素滿足函數(shù)要求就返回true,否則就返回false
var array1 = [1, 4, 9, 16];
var map9 = array1.every(function(e){
return e > 0;
})
console.log(map9); /*返回true*/
var map10 = array1.every(function(e){
return e < 10;
})
console.log(map10);/*返回false*/
語(yǔ)法
array.every(function(currentValue,index,arr),thisValue)
參數(shù)請(qǐng)參考上文的map
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組find
傳入一個(gè)函數(shù)个束,返回?cái)?shù)組中第一個(gè)符合要求的數(shù)組下標(biāo)
var array1 = [1, 4, 9, 16];
var map12 = array1.findIndex(function(e){
return e > 3;
})
console.log(map12); /*返回1慕购,元素4在數(shù)組的下標(biāo)是1*/
語(yǔ)法
array.findIndex(function(currentValue,index,arr),thisValue)
參數(shù)請(qǐng)參考上文的map
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組splice
刪除或者添加元素
var array1 = [1, 4, 9, 16];
// 在索引2的位置移除0個(gè)元素,并且插入"A"
array1.splice(2,0,"A");
console.log(array1);/*[1, 4, "A", 9, 16]*/
// 在索引2的位置移除1個(gè)元素茬底,即刪除"A"
array1.splice(2,1);
console.log(array1);/*[1, 4, 9, 16]*/
// 在索引2的位置移除1個(gè)元素沪悲,即刪除9,并在該位置添加5,6
array1.splice(2,1,5,6);
console.log(array1);/*[1, 4, 5, 6, 16]*/
語(yǔ)法
array.splice(index,howmany,item1,.....,itemX)
參數(shù)描述
index 必需。增加或者刪除的下標(biāo)起始位置
howmany 必需阱表。需要?jiǎng)h除多少個(gè)元素殿如。
如果未規(guī)定此參數(shù),則刪除從 index 開(kāi)始到原數(shù)組結(jié)尾的所有元素最爬。
item1, ..., itemX 可選涉馁。要添加到數(shù)組的新元素
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組slice
返回指定位置的一段元素。且不影響原數(shù)組
var array1 = [1, 4, 9, 16];
var map13 = array1.slice(1,3);
console.log(map13);/*[4, 9]*/
console.log(array1);/*[1, 4, 9, 16]*/
語(yǔ)法
array.slice(start, end)
參數(shù)描述爱致,以下描述來(lái)自https://www.runoob.com/jsref/jsref-slice-array.html
start 可選烤送。規(guī)定從何處開(kāi)始選取。如果是負(fù)數(shù)糠悯,那么它規(guī)定從數(shù)組尾部開(kāi)始算起的位置帮坚。也就是說(shuō)妻往,-1 指最后一個(gè)元素,-2 指倒數(shù)第二個(gè)元素叶沛,以此類推蒲讯。
end 可選。規(guī)定從何處結(jié)束選取灰署。該參數(shù)是數(shù)組片斷結(jié)束處的數(shù)組下標(biāo)判帮。如果沒(méi)有指定該參數(shù),那么切分的數(shù)組包含從 start 到數(shù)組結(jié)束的所有元素溉箕。如果這個(gè)參數(shù)是負(fù)數(shù)晦墙,那么它規(guī)定的是從數(shù)組尾部開(kāi)始算起的元素。
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組concat()
拼接多個(gè)數(shù)組肴茄。
vvar array2 = [2, 3, 4, 5];
var array3 = ["A", 3, 4, 5];
var map14 = array1.concat(array2,array3);//括號(hào)內(nèi)科增加無(wú)數(shù)個(gè)數(shù)組
console.log(map14);/*返回[1, 4, 9, 16, 2, 3, 4, 5, "A", 3, 4, 5]*/
語(yǔ)法
array1.concat(array2,array3,...,arrayX)
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組includes
查看數(shù)組中是否包含特定的值晌畅,返回true、false寡痰。
var array1 = [1, 4, 9, 16];
console.log(array1.includes(2));/*返回false*/
console.log(array1.includes(4));/*返回true*/
語(yǔ)法
arr.includes(searchElement, fromIndex)
參數(shù)描述
searchElement 必須抗楔。需要查找的元素值。
fromIndex 可選拦坠。從那個(gè)位置開(kāi)始找searchElement
----------------------------------------------------華麗的分割線----------------------------------------------------------
js中數(shù)組join()
將數(shù)組拼接成字符串返回连躏。
var array1 = [1, 4, 9, 16];
console.log(array1.join());/*1,4,9,16*/
console.log(array1.join("++"));/*1++4++9++16*/
語(yǔ)法
array.join(separator)
參數(shù)描述
separator 可選。以什么元素拼接贞滨,默認(rèn)逗號(hào)
----------------------------------------------------華麗的分割線----------------------------------------------------------