JavaScript中常用的數(shù)組操作方法
一、concat()
concat()
方法用于連接兩個或多個數(shù)組。該方法不會改變現(xiàn)有的數(shù)組,僅會返回被連接數(shù)組的一個副本。
var arr1 = [1,2,3]; var arr2 = [4,5]; var arr3 = arr1.concat(arr2); console.log(arr1); //[1, 2, 3] console.log(arr3); //[1, 2, 3, 4, 5]
二、join()
join()
方法用于把數(shù)組中的所有元素放入一個字符串。元素是通過指定的分隔符進(jìn)行分隔的,默認(rèn)使用','號分割,不改變原數(shù)組。
var arr = [2,3,4]; console.log(arr.join()); //2,3,4 console.log(arr); //[2, 3, 4]
三、push()
push()
方法可向數(shù)組的末尾添加一個或多個元素,并返回新的長度。末尾添加,返回的是長度,會改變原數(shù)組。
var a = [2,3,4]; var b = a.push(5); console.log(a); //[2,3,4,5] console.log(b); //4
五、shift()
shift()
方法用于把數(shù)組的第一個元素從其中刪除,并返回第一個元素的值。返回第一個元素,改變原數(shù)組。
var arr = [2,3,4]; console.log(arr.shift()); //2 console.log(arr); //[3,4]
六、unshift()
unshift()
方法可向數(shù)組的開頭添加一個或更多元素,并返回新的長度。返回新長度,改變原數(shù)組。
var arr = [2,3,4,5]; console.log(arr.unshift(3,6)); //6 console.log(arr); //[3, 6, 2, 3, 4, 5]
tip:該方法可以不傳參數(shù),不傳參數(shù)就是不增加元素。
七、slice()
slice()
方法返回一個新的數(shù)組,包含從 start
到 end
(不包括該元素)的 arrayObject
中的元素。返回選定的元素,該方法不會修 改原數(shù)組。
var arr = [2,3,4,5]; console.log(arr.slice(1,3)); //[3,4] console.log(arr); //[2,3,4,5]
八、splice()
splice()
方法可刪除從 index
處開始的零個或多個元素,并且用參數(shù)列表中聲明的一個或多個值來替換那些被刪除的元素。如果從 arrayObject
中刪除了元素,則返回的是含有被刪除的元素的數(shù)組。splice()
方法會直接對數(shù)組進(jìn)行修改。
var a = [5,6,7,8]; console.log(a.splice(1,0,9)); //[] console.log(a); // [5, 9, 6, 7, 8] var b = [5,6,7,8]; console.log(b.splice(1,2,3)); //[6, 7] console.log(b); //[5, 3, 8]
九、substring() 和 substr()
相同點(diǎn):如果只是寫一個參數(shù),兩者的作用都一樣:都是是截取字符串從當(dāng)前下標(biāo)以后直到字符串最后的字符串片段。
substr(startIndex); substring(startIndex); var str = '123456789'; console.log(str.substr(2)); // "3456789" console.log(str.substring(2)) ;// "3456789"
不同點(diǎn):第二個參數(shù) substr(startIndex,lenth):
第二個參數(shù)是截取字符串的長度(從起始點(diǎn)截取某個長度的字符串); substring(startIndex, endIndex
): 第二個參數(shù)是截取字符串最終的下標(biāo) (截取2個位置之間的字符串,‘含頭不含尾')。
console.log("123456789".substr(2,5)); // "34567" console.log("123456789".substring(2,5)) ;// "345"
十、sort 排序
按照 Unicode code
位置排序,默認(rèn)升序
var fruit = ['cherries', 'apples', 'bananas']; fruit.sort(); // ['apples', 'bananas', 'cherries'] var scores = [1, 10, 21, 2]; scores.sort(); // [1, 10, 2, 21]
十一、reverse()
reverse()
方法用于顛倒數(shù)組中元素的順序。返回的是顛倒后的數(shù)組,會改變原數(shù)組。
var arr = [2,3,4]; console.log(arr.reverse()); //[4, 3, 2] console.log(arr); //[4, 3, 2]
十二、indexOf 與 lastIndexOf
indexOf
和 lastIndexOf
都接受兩個參數(shù):查找的值、查找起始位置 不存在,返回 -1 ;存在,返回位置。indexOf
是從前往后查找, lastIndexOf
是從后往前查找。 indexOf
var a = [2, 9, 9]; a.indexOf(2); // 0 a.indexOf(7); // -1 if (a.indexOf(7) === -1) { // element doesn't exist in array } lastIndexOf var numbers = [2, 5, 9, 2]; numbers.lastIndexOf(2); // 3 numbers.lastIndexOf(7); // -1 numbers.lastIndexOf(2, 3); // 3 numbers.lastIndexOf(2, 2); // 0 numbers.lastIndexOf(2, -2); // 0 numbers.lastIndexOf(2, -1); // 3
十三、every 對數(shù)組
every
對數(shù)組的每一項(xiàng)都運(yùn)行給定的函數(shù),每一項(xiàng)都返回 ture
,則返回 true
function isBigEnough(element, index, array) { return element < 10; } [2, 5, 8, 3, 4].every(isBigEnough); // true
十四、some
some
對數(shù)組的每一項(xiàng)都運(yùn)行給定的函數(shù),任意一項(xiàng)都返回 ture
,則返回 true
function compare(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(compare); // false [12, 5, 8, 1, 4].some(compare); // true
十五、filter
filter
對數(shù)組的每一項(xiàng)都運(yùn)行給定的函數(shù),返回 結(jié)果為 ture
的項(xiàng)組成的數(shù)組
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"]; var longWords = words.filter(function(word){ return word.length > 6; }); // Filtered array longWords is ["exuberant", "destruction", "present"]
十六、map
對數(shù)組的每一項(xiàng)都運(yùn)行給定的函數(shù),返回每次函數(shù)調(diào)用的結(jié)果組成一個新數(shù)組
var numbers = [1, 5, 10, 15]; var doubles = numbers.map(function(x) { return x * 2; }); // doubles is now [2, 10, 20, 30] // numbers is still [1, 5, 10, 15]
十七、forEach 數(shù)組遍歷
const items = ['item1', 'item2', 'item3']; const copy = []; items.forEach(function(item){ copy.push(item) });
ES6
新增新操作數(shù)組的方法
1、find():
傳入一個回調(diào)函數(shù),找到數(shù)組中符合當(dāng)前搜索規(guī)則的第一個元素,返回它,并且終止搜索。
const arr = [1, "2", 3, 3, "2"] console.log(arr.find(n => typeof n === "number")) // 1
2、findIndex():
傳入一個回調(diào)函數(shù),找到數(shù)組中符合當(dāng)前搜索規(guī)則的第一個元素,返回它的下標(biāo),終止搜索。
const arr = [1, "2", 3, 3, "2"] console.log(arr.findIndex(n => typeof n === "number")) // 0
3、fill():
用新元素替換掉數(shù)組內(nèi)的元素,可以指定替換下標(biāo)范圍。
arr.fill(value, start, end)
4、copyWithin():
選擇數(shù)組的某個下標(biāo),從該位置開始復(fù)制數(shù)組元素,默認(rèn)從0開始復(fù)制。也可以指定要復(fù)制的元素范圍。
arr.copyWithin(target, start, end) const arr = [1, 2, 3, 4, 5] console.log(arr.copyWithin(3)) // [1,2,3,1,2] 從下標(biāo)為3的元素開始,復(fù)制數(shù)組,所以4, 5被替換成1, 2 const arr1 = [1, 2, 3, 4, 5] console.log(arr1.copyWithin(3, 1)) // [1,2,3,2,3] 從下標(biāo)為3的元素開始,復(fù)制數(shù)組,指定復(fù)制的第一個元素下標(biāo)為1,所以4, 5被替換成2, 3 const arr2 = [1, 2, 3, 4, 5] console.log(arr2.copyWithin(3, 1, 2)) // [1,2,3,2,5] 從下標(biāo)為3的元素開始,復(fù)制數(shù)組,指定復(fù)制的第一個元素下標(biāo)為1,結(jié)束位置為2,所以4被替換成2
5、from
將類似數(shù)組的對象(array-like object
)和可遍歷(iterable
)的對象轉(zhuǎn)為真正的數(shù)組
const bar = ["a", "b", "c"]; Array.from(bar); // ["a", "b", "c"] Array.from('foo'); // ["f", "o", "o"]
6、of
用于將一組值,轉(zhuǎn)換為數(shù)組。這個方法的主要目的,是彌補(bǔ)數(shù)組構(gòu)造函數(shù) Array()
的不足。因?yàn)閰?shù)個數(shù)的不同,會導(dǎo)致 Array()
的行為有差異。
Array() // [] Array(3) // [, , ,] Array(3, 11, 8) // [3, 11, 8] Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3]
7、entries() 返回迭代器:返回鍵值對
//數(shù)組 const arr = ['a', 'b', 'c']; for(let v of arr.entries()) { console.log(v) } // [0, 'a'] [1, 'b'] [2, 'c'] //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.entries()) { console.log(v) } // ['a', 'a'] ['b', 'b'] ['c', 'c'] //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.entries()) { console.log(v) } // ['a', 'a'] ['b', 'b']
8、values() 返回迭代器:返回鍵值對的value
//數(shù)組 const arr = ['a', 'b', 'c']; for(let v of arr.values()) { console.log(v) } //'a' 'b' 'c' //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.values()) { console.log(v) } // 'a' 'b' 'c' //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.values()) { console.log(v) } // 'a' 'b'
9、keys() 返回迭代器:返回鍵值對的key
//數(shù)組 const arr = ['a', 'b', 'c']; for(let v of arr.keys()) { console.log(v) } // 0 1 2 //Set const arr = new Set(['a', 'b', 'c']); for(let v of arr.keys()) { console.log(v) } // 'a' 'b' 'c' //Map const arr = new Map(); arr.set('a', 'a'); arr.set('b', 'b'); for(let v of arr.keys()) { console.log(v) } // 'a' 'b'
10、includes
判斷數(shù)組中是否存在該元素,參數(shù):查找的值、起始位置,可以替換 ES5
時代的 indexOf
判斷方式。indexOf
判斷元素是否為 NaN
,會判斷錯誤。
var a = [1, 2, 3]; a.includes(2); // true a.includes(4); // false
到此這篇關(guān)于JavaScript中常用的數(shù)組操作方法的文章就介紹到這了,更多相關(guān)JavaScript數(shù)組操作方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript?定時器關(guān)鍵點(diǎn)及使用場景解析
這篇文章主要為大家介紹了JavaScript?定時器關(guān)鍵點(diǎn)及使用場景解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01微信小程序本地緩存數(shù)據(jù)增刪改查實(shí)例詳解
這篇文章主要介紹了微信小程序本地緩存數(shù)據(jù)增刪改查實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05微信小程序 保留小數(shù)(toFixed)詳細(xì)介紹
這篇文章主要介紹了 微信小程序 保留小數(shù)(toFixed)詳細(xì)介紹的相關(guān)資料,這里附有實(shí)例,幫助大家學(xué)習(xí)參考此部分知識,需要的朋友可以參考下2016-11-11微信小程序 圖片寬度自適應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序 圖片寬度自適應(yīng)的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-04-04