這篇文章主要介紹了js數(shù)組的常用方法有哪些的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇js數(shù)組的常用方法有哪些文章都會有所收獲,下面我們一起來看看吧。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比石屏網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式石屏網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋石屏地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
數(shù)組的常用方法有下面幾種
方法 釋義
push()
push()?法可以接收任意數(shù)量的參數(shù),并將它們添加到數(shù)組末尾,返回數(shù)組的最新?度
unshift()
unshift() 在數(shù)組開頭添加任意多個值,然后返回新的數(shù)組?度
splice()
傳?三個參數(shù),分別是開始位置、 0 (要刪除的元素數(shù)量)、插?的元素,返回空數(shù)組
concat()
?先會創(chuàng)建?個當前數(shù)組的副本,然后再把它的參數(shù)添加到副本末尾,最后返回這個新構建的數(shù)組,不會影響原始數(shù)組
pop()
pop() ?法?于刪除數(shù)組的最后?項,同時減少數(shù)組的 length 值,返回被刪除的項
shift()
shift() ?法?于刪除數(shù)組的第?項,同時減少數(shù)組的 length 值,返回被刪除的項
splice()
傳?兩個參數(shù),分別是開始位置,刪除元素的數(shù)量,返回包含刪除元素的數(shù)組
slice()
slice() ?于創(chuàng)建?個包含原有數(shù)組中?個或多個元素的新數(shù)組,不會影響原始數(shù)組
indexOf()
返回要查找的元素在數(shù)組中的位置,如果沒找到則返回 -1
includes()
返回要查找的元素在數(shù)組中的位置,找到返回 true ,否則 false
find()
返回第?個匹配的元素
數(shù)組的兩個排序方法
reverse() 反轉數(shù)組
let values = [1, 2, 3, 4, 5];
values.reverse();
alert(values); // 5,4,3,2,1
sort()
sort() ?法接受?個?較函數(shù),?于判斷哪個值應該排在前?
數(shù)組方法的基本操作
增
數(shù)組基本操作可以歸納為 增、刪、改、查,需要留意的是哪些?法會對原數(shù)組產?影響,哪些?法不會
push()
let colors = []; // 創(chuàng)建?個數(shù)組
let count = colors.push("red", "green"); // 推?兩項
console.log(count) // 2
unshift()
let colors = new Array(); // 創(chuàng)建?個數(shù)組
let count = colors.unshift("red", "green"); // 從數(shù)組開頭推?兩項
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(); // 取得最后?項
console.log(item) // green
console.log(colors.length) // 1
shift()
let colors = ["red", "green"]
let item = colors.shift(); // 取得第?項
console.log(item) // red
console.log(colors.length) // 1
splice()
let colors = ["red", "green", "blue"];
let removed = colors.splice(0,1); // 刪除第?項
console.log(colors); // green,blue
console.log(removed); // red,只有?個元素的數(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"); // 插?兩個值,刪除?個元素
console.log(colors); // red,red,purple,blue
console.log(removed); // green,只有?個元素的數(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}
關于“js數(shù)組的常用方法有哪些”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“js數(shù)組的常用方法有哪些”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:js數(shù)組的常用方法有哪些
網(wǎng)站地址:http://aaarwkj.com/article10/jeiddo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、網(wǎng)站維護、微信公眾號、軟件開發(fā)、搜索引擎優(yōu)化、
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)