当前位置:网站首页>js之遍历数组、字符串

js之遍历数组、字符串

2022-07-06 12:51:00 viceen

js之遍历数组、字符串

1、遍历数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>遍历数组</title>
</head>
<body>
<script> const arr = [11, 22, 33, 44, 55, 66] // 创建数组 // 遍历数组 for (let i = 0; i < arr.length; i++) {
       console.log(arr[i]) } // 遍历数组 for (let key in arr) {
       console.log(key + '---' + arr[key]) } // 遍历数组 for (let key of arr) {
       console.log(key); } // 遍历数组 </script>
</body>
</html>

2、遍历字符串

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>遍历字符串</title>

</head>
<body>
<script> let data1 = "你好,嗨,哈喽,Hi,Hello,非常高兴"; let arr1 = data1.split(","); // 字符串转数组(通过",") console.log("arr1:", arr1); for (let i = 0; i < arr1.length; i++) {
       console.log(arr1[i]) } </script>
</body>
</html>
原网站

版权声明
本文为[viceen]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44867717/article/details/125618388

随机推荐