js陣列方法的總結

2021-09-13 12:54:56 字數 3779 閱讀 4987

1、隊尾插入push

var colors = ["red","green"];

colors.push("black"):

console.log(colors); //["red","green","black"]

2、隊尾刪除並返回刪除的最後一項pop

var colors = ["red","green","black"];

var item = colors.pop();

console.log(item); //"black"

3、隊首插入unshift()

var colors = ["red","green"];

colors.unshift("black");

var item = colors.pop();

console.log(item); //"black"

4、隊首刪除shift()

var colors = ["red","green","black"];

colors.shift();

console.log(colors); //["green","black"]

5、陣列一新增陣列二concat()

var colors = ["red","green","black"];

var colors2 = colors.concat("yellow",["blue","brown"]);

console.log(colors); //["red","green","black"]

console.log(colors2); //["red","green","black","yellow","blue","brown"]

6、陣列的擷取slice()

只傳乙個引數:從陣列這個引數的下標開始擷取一直到陣列結束。

var colors = ["red","green","black"];

colors.slice(1); //["green","black"]

console.log(colors); //["red","green","black"]

傳兩個引數:第乙個是擷取開始的位置,第二個是擷取結束的位置

var colors = ["red","green","black","yellow","blue","brown"];

colors.slice(1,3)//從位置1開始,到位置2結束["green","black"];

7、陣列的splice()方法

有三種用法:
var colors = ["red","green","black"];

var removed = colors.splice(0,1);

console.log(colors); //["green","black"]

console.log(removed); //["red"]

removed = colors.splice(1,0,"yellow","orange");

console.log(colors); //["green","yellow","orange","black"]

console.log(removed); //

removed = colors.splice(1,1,"red","purple");

console.log(colors); //["green","red","purple","orange","black"]

console.log(removed); //["yellow"]

8、位置方法indexof()和lastindexof()

indexof()和lastindexof()都接收兩個引數,第乙個引數是要查詢的項,第二個(可選)查詢開始的位置,indexof()是從陣列頭開始查,lastindexof()是從陣列尾開始查詢。
var numbers = [1,2,3,4,5,4,3,2,1];

console.log(numbers.indexof(4)); //3

console.log(numbers.lastindexof(4)); //5

console.log(numbers.indexof(4,4)); //5

var person = ;

var people = ;

var morepeople = [person];

console.log(people.indexof(person)); //-1

console.log(morepeople.indexof(person)); //0

9、查詢find()方法

查詢符合條件的第一項

var inventory = [

,];

10、迭代方法

傳入這些方法中的函式會接收三個引數:陣列項的值、該項在陣列中的位置和陣列物件本身。

var numbers = [1,2,3,4,5,4,3,2,1];

var everyresult = numbers.every((item, index, array) => );

console.log(everyresult); //false

var someresult = numbers.some((item, index, array) => );

console.log(someresult); //true

var numbers = [1,2,3,4,5,4,3,2,1];

var filterresult = numbers.filter((item, index, array) => );

console.log(filterresult); //[3,4,5,4,3]

var numbers = [1,2,3,4,5,4,3,2,1];

var mapresult = numbers.map((item, index, array) => );

console.log(mapresult); //[2,4,6,8,10,8,6,4,2]

var numbers = [1,2,3,4,5,4,3,2,1];

numbers.foreach((item, index, array) => );

11、歸併方法reduce()和reduceright()

這兩個方法都會迭代陣列的所有項,然後構建乙個最終返回的值。

reduce()方法從陣列的第一項開始,逐個遍歷到最後。

reduceright()則從陣列的最後一項開始,向前遍歷到第一項。

var values = [1,2,3,4,5];

var sum = values.reduce((prev, cur, index, array) => );

console.log(sum); //15

var values = [1,2,3,4,5];

var sum = values.reduceright((prev, cur, index, array) =>

);console.log(sum); //15

JS陣列方法總結

1 轉換方法 tostring 返回陣列的字串表示,每個值的字串表示拼接成乙個字串,中間以逗號隔開。tolocalestring valueof var colors red blue yellow console.log colors.tolocalestring red,blue,yellow ...

js陣列方法總結

下面詳細介紹一下各個方法的基本功能。1 join join separator 將陣列的元素組起乙個字串,以separator為分隔符,省略的話則用預設用逗號為分隔符,該方法只接收乙個引數 即分隔符。var arr 1,2,3 console.log arr.join 1,2,3 console.l...

js陣列方法總結

concat 方法連線兩個或多個陣列。語法 arrayobject.concat arrayx,arrayx,arrayx 引數必須,引數可以是值,也可以是陣列 把引數陣列的值連線到陣列後 該方法不會改變現有的陣列,而是返回連線後的陣列。var arr 1,2,3 引數是兩個值 var concat...