温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

js数组的常用方法有哪些

发布时间:2022-03-14 15:26:54 来源:亿速云 阅读:131 作者:iii 栏目:web开发

这篇文章主要介绍了js数组的常用方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇js数组的常用方法有哪些文章都会有所收获,下面我们一起来看看吧。

数组的常用方法有下面几种

方法 释义

push()

push()⽅法可以接收任意数量的参数,并将它们添加到数组末尾,返回数组的最新⻓度

unshift()

unshift() 在数组开头添加任意多个值,然后返回新的数组⻓度

splice()

传⼊三个参数,分别是开始位置、 0 (要删除的元素数量)、插⼊的元素,返回空数组

concat()

⾸先会创建⼀个当前数组的副本,然后再把它的参数添加到副本末尾,最后返回这个新构建的数组,不会影响原始数组

pop()

pop() ⽅法⽤于删除数组的最后⼀项,同时减少数组的 length 值,返回被删除的项

shift()

shift() ⽅法⽤于删除数组的第⼀项,同时减少数组的 length 值,返回被删除的项

splice()

传⼊两个参数,分别是开始位置,删除元素的数量,返回包含删除元素的数组

slice()

slice() ⽤于创建⼀个包含原有数组中⼀个或多个元素的新数组,不会影响原始数组

indexOf()

返回要查找的元素在数组中的位置,如果没找到则返回 -1

includes()

返回要查找的元素在数组中的位置,找到返回 true ,否则 false

find()

返回第⼀个匹配的元素

数组的两个排序方法

reverse()    反转数组

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

values.reverse();

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

sort()

sort() ⽅法接受⼀个⽐较函数,⽤于判断哪个值应该排在前⾯

数组方法的基本操作   

数组基本操作可以归纳为 增、删、改、查,需要留意的是哪些⽅法会对原数组产⽣影响,哪些⽅法不会

push()

let colors = []; // 创建⼀个数组

let count = colors.push("red", "green"); // 推⼊两项

console.log(count) // 2

unshift()

let colors = new Array(); // 创建⼀个数组

let count = colors.unshift("red", "green"); // 从数组开头推⼊两项

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,只有⼀个元素的数组

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,只有⼀个元素的数组

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数组的常用方法有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“js数组的常用方法有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI