快捷搜索: 王者荣耀 脱发

js之遍历数组、字符串

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>
经验分享 程序员 微信小程序 职场和发展