首页 > 编程语言 > 详细

js数组的方法

时间:2020-08-21 09:31:12      阅读:66      评论:0      收藏:0      [点我收藏+]

1.push和pop

push():  把里面的内容添加到数组末尾,并返回修改后的长度。

pop():移除数组最后一项,返回移除的那个值,减少数组的length。

let ary = [2,4,6,8,10,12];
let ary1 = ary.push();
console.log(ary1) // 7
console.log(ary) // [2, 4, 6, 8, 10, 12, "新"]
let ary2 = ary.pop(); console.log(ary2) // console.log(ary) // [2, 4, 6, 8, 10, 12]

2.unshift和shift

unshift(): 将参数添加到原数组开头,并返回数组的长度 。

shift():删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined 。

let ary = [2,4,6,8,10,12];
let ary1 = ary.unshift();
console.log(ary1) // 7
console.log(ary) // ["新", 2, 4, 6, 8, 10, 12]
let ary2 = ary.shift(); console.log(ary2) // console.log(ary) // [2, 4, 6, 8, 10, 12]

3.join

join,就是把数组转换成字符串,然后给他规定个连接字符,默认的是逗号(  ,),原数组不变

let ary = [a,b,c];
console.log(ary.join()) //a,b,c
console.log(ary.join(‘‘)) //abc
console.log(ary.join( )) //a b c
console.log(ary.join(-)) //a-b-c
console.log(ary) //["a", "b", "c"]

4.sort

sort():将数组里的项从小到大排序,其比较的是字符串,没有按照数值的大小对数字进行排序,要实现这一点,就必须使用一个排序函数 改变原数组

let ary = [a, d, x, b];
console.log(ary.sort()) //["a", "b", "d", "x"]
console.log(ary) //["a", "b", "d", "x"]

let ary1 = [21, 12, 3, 14]
console.log(ary1.sort()) //[12, 14, 21, 3]

ary1.sort((a, b) => {
    return a - b
})
console.log(ary1) //[3, 12, 14, 21]

5.reverse

reverse():反转数组项的顺序。改变原数组

let ary = [a,b,c];
console.log(ary.reverse()) //["c", "b", "a"]
console.log(ary) //["c", "b", "a"]

6.concat

concat() :将参数添加到原数组中。这个方法会先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。在没有给 concat()方法传递参数的情况下,它只是复制当前数组并返回副本。原数组不变

let ary = [1,2,3];
let ary1 = ary.concat(a)
console.log(ary1) //[1, 2, 3, "a"]
console.log(ary) //[1, 2, 3]

let ary2 = ary.concat([c,d])
console.log(ary2) //[1, 2, 3, "c", "d"]
console.log(ary) //[1, 2, 3]

7.slice

slice():返回从原数组中指定开始下标到结束下标之间的项组成的新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项原数组不变

let ary = [2,4,6,10,8];
let ary1 = ary.slice(1)
console.log(ary1) //[4, 6, 10, 8]
console.log(ary) //[2, 4, 6, 10, 8]

let ary2 = ary.slice(1,2)
console.log(ary2) //[4]

8.splice 改变原数组

splice():删除、插入和替换

删除:两个参数:要删除的第一项的位置和要删除的项数

插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。第二个参数为0,返回空数组

替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。第二个参数为需要替换的项数,返回被替换的项数组

let ary = [2,4,6,8,10];
let ary1 = ary.splice(1,2)
console.log(ary1) //[4, 6]
console.log(ary) //[2, 8, 10]

let ary2 = ary.splice(1,0,78,56);
console.log(ary2) //[]
console.log(ary) //[2, 78, 56, 8, 10]

let ary3 = ary.splice(2,0,a); 
console.log(ary) //[2, 78, "a", 56, 8, 10]

let ary4 = ary.splice(3,1,c,d);
console.log(ary4) //[56]
console.log(ary) //[2, 78, "a", "c", "d", 8, 10]

9.indexOf和lastIndeOf

indexOf(): 返回数组中元素在数组中的下标位置,从下标0开始查询,若数组中不存在改元素则返回-1

lastIndexOf(): 返回数组中元素在数组中的下标位置,从末尾开始查询

let ary = [5,7,7,8,9,1,1,4];
console.log(ary.indexOf(6)) //-1
console.log(ary.indexOf(5)) //0
console.log(ary.lastIndexOf(1)) //6

10.forEach

forEach():对数组进行遍历循环,对数组中的每一项运行给定函数。这个方法没有返回值。参数都是function类型,默认有传参,参数分别为:遍历的数组的每一项,该项对应的数组下标,数组本身。

let ary = [1, 2, 3, 4, 5];
ary.forEach((item, index) => {
    if(item == 2) {
        ary[index] = 7
    }
})
console.log(ary) // [1, 7, 3, 4, 5]

11.map

map():指“映射”,对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。return返回新数组

let ary = [1, 2, 3, 4, 5];
let ary1 = ary.map((item,index)=>{
    return item*item
})
console.log(ary1) //[1, 4, 9, 16, 25]
console.log(ary) //[1, 2, 3, 4, 5]
let ary = [1, 2, 3, 4, 5];
ary.map((item, index) => {
    if (item == 2) {
        ary[index] = 7
    }
})
console.log(ary) //[1, 7, 3, 4, 5]

12.filter

filter():“过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。return返回新数组

let ary = [1, 2, 3, 4, 5];
let ary1 = ary.filter((item, index) => {
    return item > 2
})
console.log(ary1) //[3, 4, 5]
console.log(ary) //[1, 2, 3, 4, 5]
let ary = [1, 2, 3, 4, 5];
ary.filter((item, index) => {
    if (item == 2) {
        ary[index] = 7
    }
})
console.log(ary) //[1, 7, 3, 4, 5]

13.every

every():判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。

let ary = [1, 2, 3, 4, 5];
let ary1 = ary.every(item => {
    return item < 6
})
let ary2 = ary.every(item => {
    return item < 5
})
console.log(ary1) //true
console.log(ary2) //false

14.some

 some():判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。

let ary = [1, 2, 3, 4, 5];
let ary1 = ary.some(item => {
    return item < 2
})
let ary2 = ary.some(item => {
    return item < 1
})
console.log(ary1) //true
console.log(ary2) //false

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

js数组的方法

原文:https://www.cnblogs.com/qlongbg/p/13537077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!