数组的方法

1.concat() 拼接数组,连接两个或更多的数组,并返回全新的数组:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33]
        let arr2 = [44,55,66]
        let arr3 = arr1.concat(arr2)
        console.log(arr3);
    </script>
</body>

</html>

2.fill() 使用一个固定值来填充数组:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33,44]
        arr1.fill(100)
        console.log(arr1);
    </script>
</body>

</html>

3.includes() 判断一个数组是否包含一个指定的值,包含返回true,否则返回false:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33,44]
        console.log(arr1.includes(11));
        console.log(arr1.includes(55));
    </script>
</body>

</html>

4.indexOf() 搜索数组中的指定元素第一次出现的位置,如果没有返回-1。注意:位置就是下标,从0开始。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33,44,22,55,66,22,77,88]
        console.log(arr1.indexOf(22));
        console.log(arr1.indexOf(222));
    </script>
</body>

</html>

5. lastIndexOf() 搜索数组中的指定元素最后一次出现的位置,如果没有返回-1。注意:位置就是下标,从0开始。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33,44,22,55,66,22,77,88]
        console.log(arr1.lastIndexOf(22));
        console.log(arr1.lastIndexOf(222));
    </script>
</body>

</html>

6.isArray() 判断某个对象是否是数组对象:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = [11,22,33,44,22,55,66,22,77,88]
        console.log(Array.isArray(arr1));
        let num1 = 100
        console.log(Array.isArray(num1));
    </script>
</body>

</html>

7.join() 用于将数组中的元素,拼接成一个字符串返回。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       let arr1 = ['鹿晗','吴亦凡','张艺兴','黄子韬']
        // join() 方法不传参数,默认是根据逗号字符拼接。
        let str1 = arr1.join()
        console.log(str1);
    </script>
</body>

</html>

8.push() 向数组的末尾添加一个或更多元素,并返回新的长度:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       let arr1 = ['鹿晗','吴亦凡','张艺兴','黄子韬']
        arr1.push('周杰伦','刘德华')
        console.log(arr1);
    </script>
</body>

</html>

9.pop() 删除数组的最后一个元素并返回删除的元素。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       let arr1 = ['鹿晗','吴亦凡','张艺兴','黄子韬']
        let arr2 = arr1.pop()
        // 返回新的数组
        console.log(arr1);
        // 返回删除的元素
        console.log(arr2);
    </script>
</body>

</html>

10.unshift() 向数组的开头添加一个或更多元素,并返回新的长度:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       let arr1 = ['鹿晗','吴亦凡','张艺兴','黄子韬']
       arr1.unshift('王源')
       console.log(arr1);
    </script>
</body>

</html>

11.shift() 删除并返回数组的第一个元素:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = ['鹿晗', '吴亦凡', '张艺兴', '黄子韬']
        let s1 = arr1.shift()
        // 返回新的数组
        console.log(arr1);
        // 返回被删除的元素
        console.log(s1);
    </script>
</body>

</html>

12.reverse() 反转数组的元素顺序:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr1 = ['鹿晗', '吴亦凡', '张艺兴', '黄子韬']
        arr1.reverse()
        console.log(arr1);
    </script>
</body>

</html>

13.slice() 选取数组的一部分,并返回一个新数组:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let arr1 = ['a','b','c','d','e','f','g','h','i','j','k']
        // slice() 方法,需要传两个参数,分别是:开始位置和结束位置。注意:能取到开始位置,取不到结束位置。
        let arr2 = arr1.slice(3,7)
        console.log(arr2);
    </script>
</body>

</html>

14.splice() 从数组中添加或删除元素,注意:会将删除后的元素,保存到一个新的数组中并返回。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
         let arr1 = ['鹿晗','吴亦凡','张艺兴','黄子韬','刘青云','吴彦祖']
         // splice() 方法,需要传两个参数,分别是:开始位置和删除长度。
        let arr2 = arr1.splice(3,4)
        console.log(arr1);
        console.log(arr2);
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值