日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

splice方法_JavaScript数组常用方法

發布時間:2025/4/16 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 splice方法_JavaScript数组常用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

狀態不好,感覺還是進度太慢了,自己總是覺得自己不夠努力,,,忽然覺得為什么要努力呢。。。一想到這里,我就意志消沉,看不到盡頭的感覺。

Array類型

1. 創建

var colors = new Array(); var colors = new Array(3); var colors = new Array("blue,black,red"); var colors = Array(); var colors = Array(3); var colors = Array("blue,black,red"); var colors = []; var colors = ["blue","black","red"];

常見屬性: length

colors.length;

2. 檢測數組

確定某值在不在數組中,Array.isArray(arg)返回true或false。

var colors = ["blue","black","red"]; var a = Array.isArray(colors); console.log(a);//true

3. 轉換方法

如前所述:有三個方法。

toString(): 返回數組中每個值的字符串形式,調用toString(),并拼接一個以,分隔的字符串。

toLocalString() 取得每一項的值使用的是每一項的toLocalString()而不是toString()

valueOf():調用ToObject方法,并將this的值作為參數傳入。數組是對象,所以直接返回數組本身。

join()方法,給出不同的分割符。

var colors = ["blue","black","red"]; console.log(colors.toLocaleString());//blue,black,red console.log(colors.toString());//blue,black,red console.log(colors.valueOf());//[ 'blue', 'black', 'red' ] console.log(colors);//[ 'blue', 'black', 'red' ]

4. 操作數組

push(),接受參數,將其添加到數組的末尾。

var colors = ["blue","black","red"]; colors.push("red"); colors.push("yellow","orange"); console.log(colors);//[ 'blue', 'black', 'red', 'red', 'yellow', 'orange' ]

pop()返回數組最后一項,并刪去。

var colors = ["blue","black","red"]; colors.push("red"); colors.push("yellow","orange"); console.log(colors);//[ 'blue', 'black', 'red', 'red', 'yellow', 'orange' ] colors.pop(); var x = colors.pop(); console.log(colors);//[ 'blue', 'black', 'red', 'red' ] console.log(x);//yellow ?

shift(),返回數組的第一項,并刪去。繼續上面操作

var y = colors.shift(); console.log(y);//blue console.log(colors);//[ 'black', 'red', 'red' ]

unshift(),在數組的前端加入元素(1個或多個)。具體不演示。

reverse(),反轉數組的順序,返回一個新的數組,在原數組上面做更改。

var colors = ["blue","black","red"]; var color2 = colors.reverse(); color2 = ["cno"]; console.log(color2);//[ 'cno' ] console.log(colors);//[ 'red', 'black', 'blue' ]

sort()方法。按照升序排列數組項。調用數組每一項的toString()方法,然后比較字符串,即使是數值??梢灾付ê瘮底鳛閰?。比較函數有兩個參數,如果第一個參數位于第二個參數之前,返回復數,如果相等,返回0,否則返回一個正數。同樣返回的是一個新排序的數組,并且原數組改變。

function compare(num1, num2) {if (num1 < num2) {return -1;} else if (num1 === num2) {return 0;} else {return -1;} } ? var nums = [0, 1, 3, 4, 5, 6]; console.log(nums.sort(compare));//由于只是用作參數,所以不需要compare()調用。 console.log(nums);//[ 0, 1, 3, 4, 5, 6 ]

5. 操作方法

concat()基于當前數組中的所有項創建一個新的數組。首先創建一個當前數組的副本,再將接收到的參數添加到副本的末尾,最后返回新構建的數組。原來數組不變。

var colors = ["blue","yellow","red"]; var colors2 = colors.concat();//沒有給參數,則是復制一個新數組。 console.log(colors2);//[ 'blue', 'yellow', 'red' ] var colors3 = colors.concat("black");//給定一個參數,會將這個參數放在后面去。 console.log(colors3);//[ 'blue', 'yellow', 'red', 'black' ] var colors4 = colors.concat(["yellow","black"]);//將數組里的每一項,添加到末尾。 console.log(colors4);//[ 'blue', 'yellow', 'red', 'yellow', 'black' ] var colors5 = colors.concat("pink",["yellow","black"]); console.log(colors5);//[ 'blue', 'yellow', 'red', 'pink', 'yellow', 'black' ] console.log(colors);//[ 'blue', 'yellow', 'red' ]

slice()基于當前數組中的一或多個項創建一個新數組。slice()方法返回一個從指定位置開始,到指定位置結束的數組(包左不包右原則)。原數組不變。

var colors = ["blue","yellow","red"]; var colors2 = colors.slice(1); var colors3 = colors.slice(1,1); var colors4 = colors.slice(1,2); console.log(colors2);//[ 'yellow', 'red' ] console.log(colors3);//[] console.log(colors4);//[ 'yellow' ]

splice()方法,第一個參數:起始位置。第二個參數:刪除的項數。第三個參數插入的項,第四個參數要插入的項,,,,返回刪除那些項組成的數組,改變原數組。

var colors = ["blue","yellow","red"]; // colors.splice(0,1);//[ 'yellow', 'red' ] 可以當做刪除元素 // colors.splice(0,2,"pink","玫瑰金","騷氣紅"); //[ 'pink', '玫瑰金', '騷氣紅', 'red' ]可以當做替換 // colors.splice(1,0,"玫瑰白");//可以當做替換。 colors.splice(1,0,["炫彩藍","玫瑰金"]);//[ 'blue', [ '炫彩藍', '玫瑰金' ], 'yellow', 'red' ] console.log(colors);

6. 位置方法

indexOf()和lastIndexOf():接受兩個參數,第一個:要查找的項 和 (可選的)查找起點位置的索引。

7. 迭代方法

以下方法,接受兩個參數,在每一項上運行的函數和(可選的)運行該函數的作用于對象--影響this的值。傳入這些方法的函數會接受三個參數:數組項的值、該項在是數組中的位置和數組對象本身。

every()對數組紅的每一項運行給定函數,如果該函數對每一項都返回true,則返回true。

filter()對數組中的每一項運行給定函數,返回該函數會返回true的項組成的數組。

forEach()對數組中的每一項運行給定函數。這個方法沒有返回值;

map()對數組中的每一項運行給定函數。返回每次函數調用的結果組成的數組。

some()對數組中的每一項運行給定函數,如果該函數對任一項返回true,則返回true。

以上各個方法不會修改原數組的值。

var numbers = [1, 2, 3, 4, 5, 6, 7, 4, 2, 3]; var everyResult = numbers.every(function (item, index, array) {return (item > 2); }); console.log(everyResult);//false var someResult = numbers.some(function (item, index, array) {return (item > 2); }); console.log(someResult);//true var filterResult = numbers.filter(function (item, index, array) {return (item > 2); }); console.log(filterResult);//[ 3, 4, 5, 6, 7, 4, 3 ] var mapResult = numbers.map(function (item, index, array) {return item * 2; }); console.log(mapResult);//[ 2, 4, 6, 8, 10, 12, 14, 8, 4, 6 ]

8. 歸并方法

reduce()和`reduceRight()`

接受兩個參數,一個在每一項調用的函數 和(可選的)作為歸并基礎的初始值。傳給reduce()和reduceRight()的函數接受四個參數:前一個值、當前值、項的索引和數組對象。

var numbers = [1, 2, 3, 4, 5, 6, 7, 4, 2, 3]; var sum = numbers.reduce(function(prev,cur,index,array) {return prev+cur; }); console.log(sum);//37

總結

以上是生活随笔為你收集整理的splice方法_JavaScript数组常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。