js数组对象方法详细总结

<!-- <script>

        // 1.数组.push("追加的内容1","追加的内容2","追加的内容3"...),把追加的内容在数组的最后开始,按顺序追加,

        // 返回值是追加以后的数组长度,直接操作原始数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.push("追加的内容1","灰太狼")

        // 得到的结果是6

        console.log(res)

        // 得到的结果是(6) ['hello', 'world', '你好', '世界', '追加的内容1', '灰太狼']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 2.数组.pop(),没有参数,删除数组的最后一个数值.返回值是删除的数值,直接操作原始的数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.pop()

        // 返回值是:世界

        console.log(res)

        // 得到的结果是:(3) ['hello', 'world', '你好']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 3.数组.unshift("追加的内容1","追加的内容2","追加的内容3"...),把追加的内容在数组的最前面开始,按顺序追加,

        // 返回值是追加以后的数组长度,直接操作原始数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.unshift("追加的内容1","灰太狼")

        // 得到的结果是6

        console.log(res)

        // 得到的结果是(6) [ '追加的内容1', '灰太狼','hello', 'world', '你好', '世界']

        console.log(arr)

    </script> -->

    <!-- push和unshift的区别:一个在后面添加,一个在前面添加,unshift的方法会改变原始数组的索引 -->

    <!-- <script>

        // 4.数组.shift(),没有参数,删除数组最前面的一个数值.返回值是删除的数值,直接操作原始的数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.shift()

        // 返回值是:hello

        console.log(res)

        // 得到的结果是:(3) ["world","你好","世界"]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 5.数组.reverse(),没有参数,将原有数组的排序反过来,返回值是反转后得到的数组,直接操作原有数组.

        var arr = ["hello","world","你好","世界"]

        var res = arr.reverse()

        // 返回值是:(4) ['世界', '你好', 'world', 'hello']

        console.log(res)

        // 得到的结果和返回值一样:(4) ['世界', '你好', 'world', 'hello']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 6-1.sort(),没有参数,将数组按照ASCII码排列,返回值是得到的排列的数组,直接操作原有数组.

        var arr = ["5","51","2","1","11","12"]

        var res = arr.sort()

        // 返回值是:(6) ['1', '11', '12', '2', '5', '51']

        console.log(res)

        // 得到的结果和返回值一样:(6) ['1', '11', '12', '2', '5', '51']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 6-2.sort(function (a,b){ return a-b; }),将数组按照从小到大排列,返回值是得到的排列的数组,直接操作原有数组.

        var arr = ["5","51","2","1","11","12"]

        var res = arr.sort(function (a,b){ return a-b; })

        // 返回值是:(6) ['1', '2', '5', '11', '12', '51']

        console.log(res)

        // 得到的结果和返回值一样:(6) ['1', '2', '5', '11', '12', '51']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 6-3.sort(function (a,b){ return b-a; }),将数组按照从大到小排列,返回值是得到的排列的数组,直接操作原有数组.

        var arr = ["5","51","2","1","11","12"]

        var res = arr.sort(function (a,b){ return b-a; })

        // 返回值是:(6) ['51', '12', '11', '5', '2', '1']

        console.log(res)

        // 得到的结果和返回值一样:(6) ['51', '12', '11', '5', '2', '1']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 7 数组.splice()

        // 作用有两个:1.截取数组  2.替换新内容

        // 返回值:一定是一个数组.  1.如果截取多个数据,返回的就是一个数据多的数组    2.如果截取的是一个数据,

        // 返回的就是一个数据的数组 3.如果没有截取,返回的是空数组

        // 直接操作原始数组

        // 语法1. 数组.splice(开始索引,多少个),第二个参数可以不写,直接从索引位置到最后

        // 语法2. 数组.splice(开始索引,多少个,替换的值1,替换的值2,替换的值3...),注意:把替换数据按照顺序插入到截取

        // 索引的位置.从哪个位置开始删除,第一个替换的值就插到那个索引的位置

    </script> -->

    <!-- <script>

        // 7 语法1

        var arr = ["hello","world","你好","世界"]

        // 意思是从索引1的位置开始,截取两个数值

        var res = arr.splice(1,2)

        // 返回值是:(2) ['world', '你好']

        console.log(res)

        // 得到的结果是:(2) ['hello', '世界']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 7 语法2

        var arr = ["hello","world","你好","世界"]

        // 意思是从索引1的位置开始,截取两个数值,增加数值

        var res = arr.splice(1,2,"22","灰太狼","33")

        // 返回值是:(2) ['world', '你好']

        console.log(res)

        // 得到的结果是:(5) ['hello', '22', '灰太狼', '33', '世界']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 8 数组.concat("数组","数据"....)

        // 作用:连接数组,如果里面是数组,就把数组拆开,在排到原始数组后面,如果是数据,直接排到原始数组后面

        // 返回值:返回连接后的数组.

        // 不影响原始数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.concat(['22', '灰太狼'],"小灰灰")

        // 返回值是:(7) ['hello', 'world', '你好', '世界', '22', '灰太狼', '小灰灰']

        console.log(res)

        // 得到的结果是原始数组:(4) ['hello', 'world', '你好', '世界']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 9. 数组.slice()

        // 语法1:数组.slice(开始索引,结束索引)  包前不包后

        // 语法2:数组.slice(开始索引,结束索引),可以是负整数,相当于数组.length+负整数的位置

        // 作用:获取原始数组中的数据

        // 返回值:一定是一个数组.  1.如果截取多个数据,返回的就是一个数据多的数组    2.如果截取的是一个数据,

        // 返回的就是一个数据的数组 3.如果没有截取,返回的是空数组

        // 不影响原始数组

    </script> -->

    <!-- <script>

        // 9-1 语法1

        var arr = ["hello","world","你好","世界"]

        // 意思是:从索引一的位置开始,到索引3的位置,包前不包后

        var res = arr.slice(1,3)

        // 返回值是:(2) ['world', '你好']

        console.log(res)

        // 得到的结果是:(4) ['hello', 'world', '你好', '世界']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 9-2 语法2

        var arr = ["hello","world","你好","世界"]

        // 意思是:从索引一的位置开始,到数组.length+-1=3,包前不包后

        var res = arr.slice(1,-1)

        // 返回值是:(2) ['world', '你好']

        console.log(res)

        // 得到的结果是:(4) ['hello', 'world', '你好', '世界']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 10 数组.join("连接符号")

        // 作用:将数组的每一个数据用连接符连接

        // 返回值:是String类型的

        // 不改变原始数组

        var arr = ["hello","world","你好","世界"]

        var res = arr.join("---")

        // 返回值:hello---world---你好---世界

        console.log(res)

        // 返回值的类型:string

        console.log(typeof res)

        // 得到的结果是:(4) ['hello', 'world', '你好', '世界']

        console.log(arr)

    </script> -->

<!-- <script>

        // 11 数组.indexOf(数据)

        // 方法1:数组.indexOf(数据)

        // 方法2:数组.indexOf(数据,开始索引)

        // 作用:正向检查原始数组中有没有这个数据,得到第一个相同数据的索引

        // 返回值:有相同的,返回第一个相同数据的索引,如果没有就返回-1

        // 不影响原始数组

    </script> -->

    <!-- <script>

        // 11-1:方法1

        var arr = ["hello","world","你好","世界","你好"]

        var res = arr.indexOf("你好")

        // 返回值是索引的位置:2

        console.log(res)

        res = arr.indexOf("灰太狼")

        // 返回值为:-1

        console.log(res)

        // 得到的结果是:(5) ['hello', 'world', '你好', '世界', '你好']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 11-2:方法2

        var arr = ["hello","world","你好","世界","你好"]

        // 表示从索引3开始向后查找

        var res = arr.indexOf("你好",3)

        // 返回值是索引的位置:4

        console.log(res)

        res = arr.indexOf("灰太狼")

        // 返回值为:-1

        console.log(res)

        // 得到的结果是:(5) ['hello', 'world', '你好', '世界', '你好']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 12 数组.lastIndexOf(数据)

        // 方法1:数组.lastIndexOf(数据)

        // 方法2:数组.lastIndexOf(数据,开始索引)

        // 作用:反向检查原始数组中有没有这个数据,得到第一个相同数据的索引

        // 返回值:有相同的,返回第一个相同数据的索引,如果没有就返回-1,这里注意:***返回的索引是原始数组的索引值***

        // 不影响原始数组

    </script> -->

    <!-- <script>

        // 12-1:方法1

        var arr = ["hello","world","你好","世界","你好"]

        var res = arr.lastIndexOf("你好")

        // 返回值是索引的位置:4

        console.log(res)

        res = arr.indexOf("灰太狼")

        // 返回值为:-1

        console.log(res)

        // 得到的结果是:(5) ['hello', 'world', '你好', '世界', '你好']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 12-2:方法2

        var arr = ["hello","world","你好","世界","你好"]

        // 表示从正向的索引3开始向左查找

        var res = arr.lastIndexOf("你好",3)

        // 返回值是索引的位置:2

        console.log(res)

        res = arr.indexOf("灰太狼")

        // 返回值为:-1

        console.log(res)

        // 得到的结果是:(5) ['hello', 'world', '你好', '世界', '你好']

        console.log(arr)

    </script> -->

    <!-- <script>

        // 13 数组.forEach(函数)   注意:forEach没有for循环遍历数组好,比for更加浪费性能

        // 用法: 数组.forEach( function (item,index,arr){} ),item表示数组的每一项.index表示数组每一项的索引.arr表示原始数组

        // 作用:用来遍历数组.取代for循环,进行遍历数组

        // 没有返回值

        var arr = ["hello","world","你好","世界"]

        // 得到的结果是:0---hello---hello,world,你好,世界  1---world---hello,world,你好,世界  

        // 2---你好---hello,world,你好,世界 3---世界---hello,world,你好,世界

        arr.forEach(function (item,index,arr){

            // index,代表的是数组每一项的索引,item表示数组的每一项的数据,arr表示原始数组

            console.log(index+"---"+item+"---"+arr)

        })

    </script> -->

    <!-- <script>

        // 14 数组.map(函数)

        // 用法:数组.map(function (item,index,arr){})

        // item表示数组的每一项.index表示数组每一项的索引.arr表示原始数组

        // 作用:映射数组

        // 返回值:是一个新的数组,里面是对每个原始数组数据的操作.注意**返回的数组一定和原始数组的长度一样**

        // 不改变原始数组

        var arr = [10,100,1000]

        var res = arr.map(function (item,index,arr){

            // 得到的结果是:10 '===' 0 '====' (3) [10, 100, 1000]

                            // 100 '===' 1 '====' (3) [10, 100, 1000]

                            //   1000 '===' 2 '====' (3) [10, 100, 1000]

            // console.log(item,"===",index,"====",arr)

            return item*1.3

        })

        // 得到的结果是:(3) [undefined, undefined, undefined]

        // return之后得到的结果是:(3) [13, 130, 1300]

        console.log(res)

      </script>

<!-- <script>

        // 15 filter()

        // 用法:数组.filter(function (item,index,arr){})

        // item表示数组的每一项.index表示数组每一项的索引.arr表示原始数组

        // 作用:过滤原始数组中的数据,把满足条件的放在新数组里面

        // 返回值:新数组,里面是所有满足过滤条件的项

        // 不改变原始数组

        var arr = [10,20,30,40,50]

        var res = arr.filter(function (item,index,arr){

            return item>30

        })

        // 返回值是:(2) [40, 50]

        console.log(res)

        // 原始数组不变:(5) [10, 20, 30, 40, 50]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 16 every()

        // 用法:数组.every(function (item,index,arr){})

        // item表示数组的每一项.index表示数组每一项的索引.arr表示原始数组

        // 作用:判断原始数组中的每一个数据都符合条件不符合。

        // 返回值是Boolean,布尔值  

        // 不改变原始数组

        var arr = [10,20,30,40,50]

        var res = arr.every(function (item,index,arr){

            // console.log(item,"----",index,"-----",arr)

            return item<20

        })

        // 返回值:false,当条件不符合的时候就跳出循环。

        console.log(res)

        // 返回值的类型是:boolean

        console.log(typeof res)

        // 原始数组不变:(5) [10, 20, 30, 40, 50]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 17 some()

        // 用法:数组.some(function (item,index,arr){})

        // item表示数组的每一项.index表示数组每一项的索引.arr表示原始数组

        // 作用:判断原始数组中的数据只要有一个符合条件既可以。

        // 返回值是Boolean,布尔值,有一个满足条件返回true,都不满足返回false  

        // 不改变原始数组

        var arr = [10,20,30,40,50]

        var res = arr.some(function (item,index,arr){

            // console.log(item,"----",index,"-----",arr)

            return item<20

        })

        // 返回值:true,当条件不符合的时候就跳出循环。

        console.log(res)

        // 返回值的类型是:boolean

        console.log(typeof res)

        // 原始数组不变:(5) [10, 20, 30, 40, 50]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 18 数组.copyWithin()

        // 用法: 数组.copyWithin(替换的索引位置,替换的开始内容的索引位置,替换的结束内容的索引位置 )  替换的开始内容的索引和替换的

        // 结束内容的索引,包前不包后

        // 作用:用原始数组的开始结束内容索引的内容替换原始数组中某个索引位置开始的内容

        // 返回值:替换后的数组

        // 改变原始数组

        var arr = [2,3,45,664,22,332,12,11]

        // 这句话的意思是:从索引1开始用原始中的数组来替换原始数组中的内容,替换的内容范围是:索引4到索引6,包前不包后。就是索引4 22,

        // 索引5 332

        var res = arr.copyWithin(1,4,6)

        // 返回值:(8) [2, 22, 332, 664, 22, 332, 12, 11]

        console.log(res)

        // 原始数组:(8) [2, 22, 332, 664, 22, 332, 12, 11]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 19 fill()

        // 用法: 数组.fill(要填充的数据,开始索引,结束索引)

        // 前提:数组要有长度,否则无法填充,包前不包后

        // 作用:使用指定数据去填充数组

        // 返回值:填充好的数组

        // 改变原始数组

        // 创建一个长度为30的空数组

        var arr = new Array(30);

        var res = arr.fill("灰太狼",0,5)

        // 返回值:(30) ['灰太狼', '灰太狼', '灰太狼', '灰太狼', '灰太狼', empty × 25]

        console.log(res)

        // 原始数组改变:(30) ['灰太狼', '灰太狼', '灰太狼', '灰太狼', '灰太狼', empty × 25]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 20 includes()

        // 用法: 数组.includes()

        // 作用: 判断原始数组中是否有该数据

        // 返回值:boolean,true  false

        // 原始数组不该变

        var arr = [2,3,45,664,22,332,12,11]

        var res =arr.includes(12)

        // 返回值:true

        console.log(res)

        // 原始数组不改变:(8) [2, 3, 45, 664, 22, 332, 12, 11]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 21 flat()

        // 用法:数组.flat(拍平数组的层数)   嵌套的层数  ,这里的参数可以是Infinity,无穷。也可以是-Infinity,负无穷

        // 作用:把数组中的二维数组,数组中的数组的数组,统统展开

        // 返回值:拍平后的数组

        // 原始数组不改变

        var arr = [

            10,20,30,

            [40,50],

            [60,70,[80,[90]]]

        ]

        // 得到的结果是:(5) [10, 20, 30, Array(2), Array(3)]

        // console.log(arr)

        // var res = arr.flat(2)

        // 得到的结果是:(9) [10, 20, 30, 40, 50, 60, 70, 80, Array(1)]

        // console.log(res)

        // 原始数组:(5) [10, 20, 30, Array(2), Array(3)]

        // console.log(arr)

        // 在2018年之前没有这个方法,用的是toString()

        var res1 = arr.toString().split(",")

        // 返回值:['10,20,30,40,50,60,70,80,90']。toString()将数组变成字符串,split("英文逗号")是将字符串变成数组

        console.log(res1)

    </script> -->

    <!-- <script>

        // 22 flatMap()   不常用,几乎不用

        // 用法:数组.flatMap(function (item,index,arr){})

        // 作用:边拍平一层,边映射,前提:原始数组中必须都是二维数组。

        // 返回值:返回一个新的数组

        // 原始数组不改变

        var arr = [[10,21,30],[40,51,60]]

        var res = arr.flatMap(function (item,index,arr){

            return item.filter(function (item){

                return item % 2 === 0

            })

        })

        // 返回值是:(4) [10, 30, 40, 60]

        console.log(res)

        // 原始数组:(2) [Array(3), Array(3)]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 23 find(function (item){})

        // 用法:数组.find(function (item){})

        // 作用:找到满足条件的原始数组的第一个数据

        // 返回值:满足条件的第一个数据

        // 原始数组

        var arr = [2,3,45,664,22,332,12,11]

        var res = arr.find(function (item){

            return item>30

        })

        // 返回值:45

        console.log(res)

        // 原始数组不改变:(8) [2, 3, 45, 664, 22, 332, 12, 11]

        console.log(arr)

    </script> -->

    <!-- <script>

        // 24 findIndex()

        // 用法:数组.findIndex(function (item){})

        // 作用:找到满足条件的原始数组的第一个数据的索引

        // 返回值:满足条件的第一个数据的索引

        // 原始数组

        var arr = [2,3,45,664,22,332,12,11]

        var res = arr.findIndex(function (item){

            return item>30

        })

        // 返回值:2

        console.log(res)

        // 原始数组不改变:(8) [2, 3, 45, 664, 22, 332, 12, 11]

        console.log(arr)

    </script> -->

  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值