前端第二周,Javascript--数组的方法(不会改变原数组的方法)

本文详细介绍了JavaScript中的数组方法,包括concat用于合并数组、toSting和toString用于转换数组为字符串、join拼接数组、indexOf和lastIndexOf查找索引、slice截取数组、forEach遍历数组以及filter和map进行过滤和映射操作,展示了这些方法的使用实例和特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组.concat()  写

             作用:实现将多个数组合并为一个数组

             返回值:多个数组合并后的新数组

             是否会改变原数组:不会

举个栗子:

 var arr1 = [1, 2, 3, 4, 5];

        console.log(arr1);

        var arr2 = ['abc', 'hello', 'world', 'xyz'];

        console.log(arr2);

        var arr3 = [true, false];

        console.log(arr3);

        var arr4 = [111, 'aaa'];

        console.log(arr4);

        var narr1 = arr1.concat(arr2, arr3);

        console.log(narr1);// [1, 2, 3, 4, 5, 'abc', 'hello', 'world', 'xyz', true, false]

        var narr2 = arr4.concat(arr3, arr2);

        console.log(narr2);//[111, 'aaa', true, false, 'abc', 'hello', 'world', 'xyz']

        console.log(arr1);

        console.log(arr4);

数组.toSting() 不写   转换

             作用:实现将数组转换为字符串

             返回值:转换后的字符串

             是否改变原数组:不会  

举个栗子:   

var arr1 = [111, 666, 999, 'hello', true];

        console.log(arr1);

        var str1 = arr1.toString();

        console.log(str1);//'111,666,999,hello,true'

        console.log(arr1);//// [111,666,999,'hello',true];

数组.join() 写   拼接

             作用:通过指定字符实现将数组的元素拼接成字符串

             返回值:拼接后的数组

             是否改变原数组:不会

举个栗子:   

  var arr2 = [111, 666, 999, 'hello', true];

        console.log(arr2);

        //如果不传入参数,和toString()方法的结果一样

        var s1 = arr2.join();

        console.log(s1);// '111,666,999,hello,true'

        var s2 = arr2.join(7);

        console.log(s2);//111766679997hello7true

        var s3 = arr2.join('abc');

        console.log(s3);//111abc666abc999abchelloabctrue

        var s4 = arr2.join(' ');

        console.log(s4);//111 666 999 hello true

        var s5 = arr2.join('')

        console.log(s5);//111666999hellotrue

        console.log(arr2);//[111, 666, 999, 'hello', true]

数组.indexOf() 写

              作用:查找指定数字在数组中第一次出现的索引位置

              返回值:

                 如果存在--返回 指定字符 在 数组中第一次出现的索引位置

                 如不存在--返回 -1

              是否会改变原数组:不会

 举个栗子:   

    var arr1 = [34, 90, true, 'hello', 'abc', true, 34, 666, false, true, 789, 90, 444];

        console.log(arr1);

        var a = arr1.indexOf(true);

        console.log(a)//2

        var b = arr1.indexOf('hello');

        console.log(b);//3

        var c = arr1.indexOf('kun')

        console.log(c);//-1

        console.log(arr1);

数组.lastIndexOf() 写

                作用:查找指定数字在数组中最一次出现的索引位置

                返回值:

                    如果存在--返回 指定字符 在 数组中最一次出现的索引位置

                    如不存在--返回 -1

                是否会改变原数组:不会

  举个栗子:   

 var arr2 = [34, 90, true, 'hello', 'abc', true, 34, 666, false, true, 789, 90, 444];

        console.log(arr2);

        var d = arr2.lastIndexOf(true);

        console.log(d);//9

        var e = arr2.lastIndexOf('abc');

        console.log(e);//4

        var f = arr2.lastIndexOf('kun');

        console.log(f);//-1

        console.log(arr2);

数组.slice() 写数字  截取(保留)

                作用:通过指定的索引实现对数组的截取

                   第一个参数:指定开始截取数组的索引位置,包含此索引位置。

                   第二个参数:指定结束截取数组的索引位置,不包含此索引。

                         第二个参数可以是负值,数组最后一项元素索引为-1,依次向前推-2 -3 -4...

                返回值:数组被截取部分组成新的数组

                是否会改变原数组:不会

 举个栗子: 

var arr1 = ['hello', 123, 999, 'abc', true, 666, 'haha', false];

        console.log(arr1);

        //如果之传入一个参数,就是从这个索引位置开始截取到数组结束

        var a = arr1.slice(3);

        console.log(a);//['abc', true, 666, 'haha', false]

        //如果传入俩个参数,表示从第一个索引位置(包含)截取到第二个所以位置(不包含)

        var b = arr1.slice(3, 5);

        console.log(b);// ['abc', true]

        var c = arr1.slice(2, -1);

        console.log(c);//[999, 'abc', true, 666, 'haha']

        var d = arr1.slice(2, -2);

        console.log(d);//[999, 'abc', true, 666]

        console.log(arr1);//['hello', 123, 999, 'abc', true, 666, 'haha', false]

数组.forEch(fn) 写三个形参数   遍历

            作用:实现对数组进行遍历

               传入一个函数作为参数(实参)

                  第一个形参:是数组中的  元素(数据/值)

                  第二个形参:是数组中的  元素的索引

                  第三个形参:是数组本身

            返回值:没有返回值----undefined   在这里不写return关键字

            否会改变原数组:不会

 举个栗子: 

 var arr1 = ['hello', 123, 999, 'abc', true, 666, 'haha', false];

        console.log(arr1);

        var n = arr1.forEach(function (a, b, c) {

            console.log(a, b, c);

        })

        console.log(n);//undefined

        console.log(arr1);//['hello', 123, 999, 'abc', true, 666, 'haha', false]  不会改变原数组

数组.filter(fn)  写三个形参数   过滤

              作用:过滤数组中不符合指定条件的元素(数据/值)

                 传入一个函数作为参数(实参)

                    第一个形参:是数组中的  元素(数据/值)

                    第二个形参:是数组中的  元素的索引

                    第三个形参:是数组本身

              返回值:由数组中符合条件的元素(数据/值)组成的新数组    需要return关键字

              否会改变原数组:不会

  举个栗子: 

 //找<=40的数字

        var arr1 = [58, 93, 21, 17, 44, 61, 73, 41, 23];

        console.log(arr1);

        var a = arr1.filter(function (item, index, narr) {

            //指定的过滤条件

            //条件结果为true(符合)的值  保留

            //条件结果为false(不符合)的值  过滤掉

            return item <= 40;

        })

        console.log(a);//[21, 17, 23]

        console.log(arr1);// [58, 93, 21, 17, 44, 61, 73, 41, 23]

        //找是偶数的数字

        var b = arr1.filter(function (item) {

            return item % 2 == 0;

        })

        console.log(b);//[58, 44]

数组.map(fn)  写三个形参数   映射

            作用:映射,实现将数组中的每一个元素都执行相同的指定操作

                传入一个函数作为参数(实参)

                    第一个形参:是数组中的  元素(数据/值)

                    第二个形参:是数组中的  元素的索引

                    第三个形参:是数组本身

            返回值:由数组中每个元素执行了相同操作后的结果组成的新数组

            否会改变原数组:不会

  举个栗子:

  var arr3 = [12, 45, 78, 34, 55];

        console.log(arr3);

        var c = arr3.map(function (item, index, narr) {

            return item *= 2;

        })

        console.log(c);//[24, 90, 156, 68, 110]

        console.log(arr3);// [12, 45, 78, 34, 55]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值