JS(JavaScript) 中 array 數(shù)組的方法

Array數(shù)組的方法

數(shù)組屬性

  1. constructor 返回創(chuàng)建數(shù)組對象的原型函數(shù)。
  2. length 設(shè)置或返回數(shù)組元素的個數(shù)。
  3. prototype 允許你向數(shù)組對象添加屬性或方法。
Array.constructor == function Array() { [native code] }
Array.length == 返回數(shù)組的長度
Array.prototype.myUcase=function()
{
    for (i=0;i<this.length;i++)
    {
        this[i]=this[i].toUpperCase();
    }
}

function myFunction()
{
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.myUcase();
    var x=document.getElementById("demo");
    x.innerHTML=fruits;
}

Array 對象方法

concat()

連接兩個或更多的數(shù)組,并返回結(jié)果

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.concat(stale,kai);
// Cecilie,Lone,Emil,Tobias,Linus,Robin

indexOf()

搜索數(shù)組中的元素,并返回它所在的位置
語法 Array.indexOf(searchElement, fromIndex)

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");//2

lastIndexOf()

搜索數(shù)組中的元素,并返回它最后出現(xiàn)的位置
語法 lastIndexOf(searchElement, fromIndex)

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf("Apple");//2

isArray()

判斷對象是否為數(shù)組

var fruits = ["Banana", "Orange", "Apple", "Mango"];
Array.isArray(fruits);// true

join()

把數(shù)組的所有元素放入一個字符串
語法 Array.join(separator)

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join();//Banana,Orange,Apple,Mango

keys()

返回數(shù)組的可迭代對象,包含原始數(shù)組的鍵(key)

for(let key of ['a', 'b'].keys()){
    console.log(key);
}
// 0
// 1
 
// 數(shù)組含空位
console.log([...[,'a'].keys()]); // [0, 1]

values()

遍歷鍵值

for(let key of ['a', 'b'].values()){
    console.log(key);
}
// 0
// 1
 
// 數(shù)組含空位
console.log([...[,'a'].values()]); // [undefined, "a"]

map()

通過指定函數(shù)處理數(shù)組的每個元素,并返回處理后的數(shù)組
語法 .map((value, index, array) => {})

var numbers = [4, 9, 16, 25];
numbers.map(Math.sqrt);//2,3,4,5

forEach()

查找數(shù)組中符合條件的元素索引,若有多個符合條件的元素,則返回第一個元素索引
語法 .forEach((currentValue, index, array) => {})

function(currentValue, index, arr)  
函數(shù)參數(shù):
參數(shù)  描述
currentValue    必需。當前元素
index   可選。當前元素的索引值。
arr 可選。當前元素所屬的數(shù)組對象。

pop()

刪除數(shù)組的最后一個元素并返回刪除的元素

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();//Banana,Orange,Apple

push()

向數(shù)組的末尾添加一個或更多元素,并返回新的長度

var fruits = ["Banana", "Orange", "Apple"];
fruits.push("1");//Banana,Orange,Apple,1

shift()

刪除并返回數(shù)組的第一個元素

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift()//Orange,Apple,Mango

unshify()

把數(shù)組轉(zhuǎn)換為字符串,并返回結(jié)果

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
//Lemon,Pineapple,Banana,Orange,Apple,Mango

reduce()

將數(shù)組元素計算為一個值(從左到右)

var numbers = [65, 44, 12, 4];
 
function getSum(total, num) {
    return total + num;
}
numbers.reduce(getSum);//125

reduceRight()

將數(shù)組元素計算為一個值(從右到左)

var numbers = [65, 44, 12, 4];
 
function getSum(total, num) {
    return total + num;
}
numbers.reduceRight(getSum);//125

reverse()

反轉(zhuǎn)數(shù)組的元素順序

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
//Mango,Apple,Orange,Banana

slice()

選取數(shù)組的一部分,并返回一個新數(shù)組

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.slice(1,3)//Orange,Apple

splice()

從數(shù)組中添加或刪除元素

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");//Banana,Orange,Lemon,Kiwi,Apple,Mango

some()

檢測數(shù)組元素中是否有元素符合指定條件

var fruits = [1,2,3,4];
fruits.smoe(function( age ){
    return age > 2;
})//true

sort()

對數(shù)組的元素進行排序

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();//Apple,Banana,Mango,Orange
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});//1,5,10,25,40,100

toString()

把數(shù)組轉(zhuǎn)換為字符串,并返回結(jié)果

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.toString();//Banana,Orange,Apple,Mango

valueOf()

返回數(shù)組對象的原始值

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var v=fruits.valueOf();
// Banana,Orange,Apple,Mango

copyWithin()

從數(shù)組的指定位置拷貝元素到數(shù)組的另一個指定位置中。

// 參數(shù)1:被修改的起始索引
// 參數(shù)2:被用來覆蓋的數(shù)據(jù)的起始索引
// 參數(shù)3(可選):被用來覆蓋的數(shù)據(jù)的結(jié)束索引,默認為數(shù)組末尾
console.log([1, 2, 3, 4].copyWithin(0,2,4)); // [3, 4, 3, 4]
// 參數(shù)1為負數(shù)表示倒數(shù)
console.log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2]
console.log([1, 2, ,4].copyWithin(0, 2, 4)); // [, 4, , 4]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
// Banana,Orange,Banana,Orange
var fruits1 = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"];
fruits1.copyWithin(2, 0, 2);
// Banana,Orange,Banana,Orange,Kiwi,Papaya

entries()

返回數(shù)組的可迭代對象

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.entries();
/*
[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]
*/

every()

every() 方法使用指定函數(shù)檢測數(shù)組中的所有元素:如果數(shù)組中檢測到有一個元素不滿足,則整個表達式返回 false ,且剩余的元素不會再進行檢測。如果所有元素都滿足條件,則返回 true

  1. every() 不會對空數(shù)組進行檢測
  2. every() 不會改變原始數(shù)組
var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.every(checkAdult);
}

fill()

用于將一個固定值替換數(shù)組的元素。

let arr = Array.of(1, 2, 3, 4);
// 參數(shù)1:用來填充的值
// 參數(shù)2:被填充的起始索引
// 參數(shù)3(可選):被填充的結(jié)束索引,默認為數(shù)組末尾
arr.fill(0,1,2); // [1, 0, 3, 4]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Runoob");
// Runoob,Runoob,Runoob,Runoob

filter()

檢測數(shù)值元素,并返回符合條件所有元素的數(shù)組。

var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}
ages.filter(checkAdult);//32,33,40

find()

返回符合傳入測試(函數(shù))條件的數(shù)組元素。

let arr = Array.of(1, 2, 3, 4);
console.log(arr.find(item => item > 2)); // 3
 
// 數(shù)組空位處理為 undefined
console.log([, 1].find(n => true)); // undefined

findIndex()

查找數(shù)組中符合條件的元素索引,若有多個符合條件的元素,則返回第一個元素索引

let arr = Array.of(1, 2, 1, 3);
// 參數(shù)1:回調(diào)函數(shù)
// 參數(shù)2(可選):指定回調(diào)函數(shù)中的 this 值
console.log(arr.findIndex(item => item == 2)); // 1
 
// 數(shù)組空位處理為 undefined
console.log([, 1].findIndex(n => true)); //0

Array.of

將參數(shù)中所有值作為元素形成數(shù)組

let Arrayof = Array.of(1,2,3,4,5, function(){}, {1:1} );
console.log( Arrayof );

Array.from

Array.from(arrayLike[, mapFn[, thisArg]]);將參數(shù)中所有值作為元素形成數(shù)組

console.log(Array.from(1, 2, 3, 4)); // [1, 2, 3, 4]
 
// 參數(shù)值可為不同類型
console.log(Array.from(1, '2', true)); // [1, '2', true]
 
// 參數(shù)為空時返回空數(shù)組
console.log(Array.from()); // []

arrayLike

Array.from([1, 2, 3]); // [1, 2, 3]

mapFn

Array.from([1, 2, 3], (n) => n * 2); // [2, 4, 6]

thisArg

let map = {
    do: function(n) {
        return n * 2;
    }
}
let arrayLike = [1, 2, 3];
console.log(Array.from(arrayLike, function (n){
    return this.do(n);
}, map)); // [2, 4, 6]

類數(shù)組對象

一個類數(shù)組對象必須含有 length 屬性,且元素屬性名必須是數(shù)值或者可轉(zhuǎn)換為數(shù)值的字符

let arr = Array.from({
  0: '1',
  1: '2',
  2: 3,
  length: 3
});
console.log(arr); // ['1', '2', 3]
 
// 沒有 length 屬性,則返回空數(shù)組
let array = Array.from({
  0: '1',
  1: '2',
  2: 3,
});
console.log(array); // []
 
// 元素屬性名不為數(shù)值且無法轉(zhuǎn)換為數(shù)值,返回長度為 length 元素值為 undefined 的數(shù)組  
let array1 = Array.from({
  a: 1,
  b: 2,
  length: 2
});
console.log(array1); // [undefined, undefined]

擴展的方法

includes()

數(shù)組是否包含指定值!注意:與 Set 和 Map 的 has 方法區(qū)分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找鍵名

// 參數(shù)1:包含的指定值
[1, 2, 3].includes(1);    // true
 
// 參數(shù)2:可選,搜索的起始索引,默認為0
[1, 2, 3].includes(1, 2); // false
 
// NaN 的包含判斷
[1, NaN, 3].includes(NaN); // true

flat()

console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
 
// 指定轉(zhuǎn)換的嵌套層數(shù)
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
 
// 不管嵌套多少層
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
 
// 自動跳過空位
console.log([1, [2, , 3]].flat()); // [1, 2, 3]

flatMap()

先對數(shù)組中每個元素進行了的處理,再對數(shù)組執(zhí)行 flat() 方法

// 參數(shù)1:遍歷函數(shù),該遍歷函數(shù)可接受3個參數(shù):當前元素、當前元素索引、原數(shù)組
// 參數(shù)2:指定遍歷函數(shù)中 this 的指向
console.log([1, 2, 3].flatMap(n => [n * 2])); // [2, 4, 6]

復制數(shù)組 ... 擴展運算符

let arr = [1, 2],
    arr1 = [...arr];
console.log(arr1); // [1, 2]
 
// 數(shù)組含空位
let arr2 = [1, , 3],
    arr3 = [...arr2];
console.log(arr3); [1, undefined, 3]

數(shù)組緩沖區(qū)

  1. 數(shù)組緩沖區(qū)是內(nèi)存中的一段地址
  2. 定型數(shù)組的基礎(chǔ)
  3. 實際字節(jié)數(shù)在創(chuàng)建時確定,之后只可修改其中的數(shù)據(jù),不可修改大小。
let buffer = new ArrayBuffer(10);
console.log(buffer.byteLength); // 10
// 分割已有數(shù)組緩沖區(qū)
let buffer1 = new ArrayBuffer(10);
let buffer2 = buffer1.slice(1, 3);
console.log(buffer2.byteLength); // 2

數(shù)組屬性

  1. 數(shù)組緩沖區(qū)是內(nèi)存中的一段地址
  2. 定型數(shù)組的基礎(chǔ)
  3. 實際字節(jié)數(shù)在創(chuàng)建時確定,之后只可修改其中的數(shù)據(jù),不可修改大小。
let buffer = new ArrayBuffer(10);
console.log(buffer.byteLength); // 10
// 分割已有數(shù)組緩沖區(qū)
let buffer1 = new ArrayBuffer(10);
let buffer2 = buffer1.slice(1, 3);
console.log(buffer2.byteLength); // 2
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容