您好,登錄后才能下訂單哦!
1、.length----獲取數組長度;
var arr = [1,2,3,4,5];
console.log(arr.length) //5
2、shift()----刪除原數組第一項,并返回刪除元素的值;如果數組為空則返回undefined,直接在元數組上修改;
var arr= [1,2,3,4,5];
var result = arr.shift();
console.log(arr) //[2,3,4,5]
console.log(result) //1
3、unshift()----將參數添加到原數組開頭,并返回數組的長度,直接在元數組上修改;
arr = [1,2,3,4,5];
result = arr.unshift(-2,-1);
console.log(arr) //[-2,-1,1,2,3,4,5]
console.log(result) //7
4、pop()----刪除原數組最后一項,并返回刪除元素的值;如果數組為空則返回undefined,直接在元數組上修改;
arr= [1,2,3,4,5];
result = arr.pop();
console.log(arr) //[1,2,3,4]
console.log(result) //5
5、push()----將參數添加到原數組末尾,并返回數組的長度,直接在元數組上修改;
arr = [1,2,3,4,5];
result = arr.push(6,7);
console.log(arr) //[1,2,3,4,5,6,7]
console.log(result) //7
6、concat()----數組合并,返回一個新數組,不影響原數組;
arr = [1,2,3,4,5];
result = arr.concat(6,7);
console.log(arr); //[1,2,3,4,5]
console.log(result); //[1,2,3,4,5,6,7]
7、sort()----將數組從小到大排序,直接在原數組上修改;
arr = [4,5,2,3,1];
result = arr.sort();
console.log(arr); //[1, 2, 3, 4, 5]
console.log(result) //[1, 2, 3, 4, 5]
8、reverse()----將數組反序,直接在原數組上修改;
arr = [4,5,2,3,1];
result = arr.reverse()
console.log(arr); //[1, 3, 2, 5, 4]
console.log(result) //[1, 3, 2, 5, 4]
9、slice(startIndex,endIndex)----截取起始索引startIndex 到 結束索引endIndex之間的項組成新的數組,該新數組包括起始索引startIndex所對應的項,不包括結束索引endIndex所對應的項;
arr = [1,2,3,4,5];
result1 = arr.slice(2,5);
result2 = arr.slice(2);
console.log(arr) //[1,2,3,4,5]
console.log(result1) //[3,4,5]
console.log(result2) //[3,4,5]
10、join(separator)----將數組的元素組起一個字符串,以separator為分隔符,省略的話則用默認用逗號為分隔符,不影響原數組;
arr= [1,2,3,4,5];
result = arr.join("|");
console.log(arr) //[1, 2, 3, 4, 5]
console.log(result) //1|2|3|4|5
11、splice(start,deleteCount,val1,val2,...)----從start位置開始刪除deleteCount項,并從該位置起插入val1,val2,... ,直接在原數組上修改;
arr= [1,2,3,4,5];
result1 = arr.splice(2,2,7,8,9);
console.log(arr); //[1,2,7,8,9,5]
console.log(result1); //[3,4]
result2 = arr.splice(0,1); //同shift
console.log(arr); //[2,7,8,9,5]
console.log(result2); //[1]
arr.splice(0,0,-2,-1); //同unshift
console.log(arr); //[-2,-1,2,7,8,9,5]
result3 = arr.length;
console.log(result3); //7
result4 = arr.splice(arr.length-1,1); //同pop
console.log(arr); //[-2,-1,2,7,8,9]
console.log(result4); //[5]
arr.splice(arr.length,0,6,7); //同push
console.log(arr); //[-2,-1,2,7,8,9,6,7]
result5 = arr.length;
console.log(result5) //8
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。