免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

js數(shù)組的常用方法有哪些

這篇文章主要介紹了js數(shù)組的常用方法有哪些的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇js數(shù)組的常用方法有哪些文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比石屏網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式石屏網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋石屏地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

數(shù)組的常用方法有下面幾種

方法 釋義

push()

push()?法可以接收任意數(shù)量的參數(shù),并將它們添加到數(shù)組末尾,返回?cái)?shù)組的最新?度

unshift()

unshift() 在數(shù)組開頭添加任意多個(gè)值,然后返回新的數(shù)組?度

splice()

傳?三個(gè)參數(shù),分別是開始位置、 0 (要?jiǎng)h除的元素?cái)?shù)量)、插?的元素,返回空數(shù)組

concat()

?先會(huì)創(chuàng)建?個(gè)當(dāng)前數(shù)組的副本,然后再把它的參數(shù)添加到副本末尾,最后返回這個(gè)新構(gòu)建的數(shù)組,不會(huì)影響原始數(shù)組

pop()

pop() ?法?于刪除數(shù)組的最后?項(xiàng),同時(shí)減少數(shù)組的 length 值,返回被刪除的項(xiàng)

shift()

shift() ?法?于刪除數(shù)組的第?項(xiàng),同時(shí)減少數(shù)組的 length 值,返回被刪除的項(xiàng)

splice()

傳?兩個(gè)參數(shù),分別是開始位置,刪除元素的數(shù)量,返回包含刪除元素的數(shù)組

slice()

slice() ?于創(chuàng)建?個(gè)包含原有數(shù)組中?個(gè)或多個(gè)元素的新數(shù)組,不會(huì)影響原始數(shù)組

indexOf()

返回要查找的元素在數(shù)組中的位置,如果沒(méi)找到則返回 -1

includes()

返回要查找的元素在數(shù)組中的位置,找到返回 true ,否則 false

find()

返回第?個(gè)匹配的元素

數(shù)組的兩個(gè)排序方法

reverse()    反轉(zhuǎn)數(shù)組

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

values.reverse();

alert(values); // 5,4,3,2,1

sort()

sort() ?法接受?個(gè)?較函數(shù),?于判斷哪個(gè)值應(yīng)該排在前?

數(shù)組方法的基本操作   

數(shù)組基本操作可以歸納為 增、刪、改、查,需要留意的是哪些?法會(huì)對(duì)原數(shù)組產(chǎn)?影響,哪些?法不會(huì)

push()

let colors = []; // 創(chuàng)建?個(gè)數(shù)組

let count = colors.push("red", "green"); // 推?兩項(xiàng)

console.log(count) // 2

unshift()

let colors = new Array(); // 創(chuàng)建?個(gè)數(shù)組

let count = colors.unshift("red", "green"); // 從數(shù)組開頭推?兩項(xiàng)

alert(count); // 2

splice()

let colors = ["red", "green", "blue"];

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

console.log(colors) // red,yellow,orange,green,blue

console.log(removed) // []

 concat()

let colors = ["red", "green", "blue"];

let colors2 = colors.concat("yellow", ["black", "brown"]);

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

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

"brown"]

pop()

let colors = ["red", "green"]

let item = colors.pop(); // 取得最后?項(xiàng)

console.log(item) // green

console.log(colors.length) // 1

shift()

let colors = ["red", "green"]

let item = colors.shift(); // 取得第?項(xiàng)

console.log(item) // red

console.log(colors.length) // 1

splice()

let colors = ["red", "green", "blue"];

let removed = colors.splice(0,1); // 刪除第?項(xiàng)

console.log(colors); // green,blue

console.log(removed); // red,只有?個(gè)元素的數(shù)組

1234

slice()

let colors = ["red", "green", "blue", "yellow", "purple"];

let colors2 = colors.slice(1);

let colors3 = colors.slice(1, 4);

console.log(colors) // red,green,blue,yellow,purple

concole.log(colors2); // green,blue,yellow,purple

concole.log(colors3); // green,blue,yellow

splice()

let colors = ["red", "green", "blue"];

let removed = colors.splice(1, 1, "red", "purple"); // 插?兩個(gè)值,刪除?個(gè)元素

console.log(colors); // red,red,purple,blue

console.log(removed); // green,只有?個(gè)元素的數(shù)組

indexOf()

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

numbers.indexOf(4) // 3

includes()

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

numbers.includes(4) // true

find()

const people = [

 {

 name: "Matt",

 age: 27

 },

 {

 name: "Nicholas",

 age: 29

 }

];

people.find((element, index, array) => element.age < 28) // // {name:

"Matt", age: 27}

關(guān)于“js數(shù)組的常用方法有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“js數(shù)組的常用方法有哪些”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章名稱:js數(shù)組的常用方法有哪些
網(wǎng)站地址:http://m.newbst.com/article10/jeiddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)網(wǎng)站維護(hù)微信公眾號(hào)軟件開發(fā)搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司