js常见的循环方法(for, while, do-while, $.each)

前段循环详解
本文深入讲解了前端开发中常用的循环方法,包括for循环、while循环、do-while循环及jQuery的$.each()循环。通过具体示例展示了每种循环的特点和使用场景,如for循环遍历数组、while循环按条件执行、do-while循环先执行后判断以及$.each()循环遍历数组元素。

前段常见的循环方法

 

for 循环

    var arrayOne = [5, 8, 6, 4, 3, 1, 2, 9, 7];
    for (var i = 0; i < arrayOne.length; i++) {
        console.log("当前的索引为:" + i);
        console.log("当前索引的值为:" + arrayOne[i]);
    }

while 循环

只要判断条件为 true ,程序就会一直执行;

    var text = "";
    var a = 0;
    while (a < 10) {
        text = "目前a的值为" + a
        console.log(text);
        a++;
    }

Do-while 循环

注意:与while不同的是,do while先不进行判断,直接先将循环体中的代码执行一遍,然后根据while中的条件的返回值判断,如果符合条件,则再一次运行循环体,如果不符合,则执行do while外面的代码

    var text = "";
    var a = 0;
    do {
        text = "目前的值为" + a
        console.log(text);
        a++;
    } while (a < 10);

Jquery $.each() 循环 

    var arr1 = ['aa', 'bb', 'cc', 'dd'];
    $.each(arr1, function(i, val) {
        console.log("当前的索引为:" + i);
        console.log("当前索引的值为:" + val);
    })

两个关键字:  break, continue

Break的用法:

   1.   用于跳出switch-case 语法结构

   2.   用于跳出循环,break后面的代码不再执行

Continue: 

  用于结束本次循环,回到判断条件处,判断循环条件是否成立,如果成立,再进行下一次循环,如果条件不成立,再跳出当前循环,continue后面的代码也不会执行。

#!/bin/bash # Config sync script for synchronizing config macros with js_list files # Usage: ./config_sync.sh <model_name> # Example: ./config_sync.sh MR100V3 MODEL_NAME="$1" if [ -z "$MODEL_NAME" ]; then echo "Usage: $0 <model_name>" echo "Example: $0 MR100V3" exit 1 fi CONFIG_PATH="config" # Fixed config directory CONFIG_FILE="${CONFIG_PATH}/${MODEL_NAME}.config" HELP_LIST_FILE="${CONFIG_PATH}/${MODEL_NAME}/multipleLanguage/help.js_list" STR_LIST_FILE="${CONFIG_PATH}/${MODEL_NAME}/multipleLanguage/str.js_list" CONFIG_MAP_FILE="config_map.txt" if [ ! -f "$CONFIG_FILE" ]; then echo "Error: Config file $CONFIG_FILE not found!" exit 1 fi if [ ! -f "$HELP_LIST_FILE" ]; then echo "Error: Help list file $HELP_LIST_FILE not found!" exit 1 fi if [ ! -f "$STR_LIST_FILE" ]; then echo "Error: String list file $STR_LIST_FILE not found!" exit 1 fi if [ ! -f "$CONFIG_MAP_FILE" ]; then echo "Error: Config map file $CONFIG_MAP_FILE not found!" exit 1 fi echo "Synchronizing config for model: $MODEL_NAME" echo "Config file: $CONFIG_FILE" echo "Help list: $HELP_LIST_FILE" echo "String list: $STR_LIST_FILE" echo "Config map: $CONFIG_MAP_FILE" echo "----------------------------------------" # Function to check if a macro is enabled in config # Handles all formats: # - INCLUDE_XXX=y (enabled) # - # INCLUDE_XXX=y (disabled) # - # INCLUDE_XXX is not set (disabled) # - #INCLUDE_XXX is not set (disabled) is_macro_enabled() { local macro="$1" if grep -q "^${macro}=y" "$CONFIG_FILE"; then return 0 # enabled elif grep -q "^#[[:space:]]*${macro}=y\|^#.*${macro}[[:space:]]*is[[:space:]]*not[[:space:]]*set" "$CONFIG_FILE"; then return 1 # disabled (commented out) else return 2 # not found fi } # Function to process a single token in a specific file # Parameters: token, file_path, macro_enabled process_single_token() { local token="$1" local file="$2" local macro_enabled="$3" # Skip if token is empty if [ -z "$token" ]; then return fi # Determine file type for logging based on file path local file_type if [[ "$file" == *"str.js_list"* ]]; then file_type="str" else file_type="help" fi # Find lines containing this token (exact match) local found_lines=$(grep -n "[[:space:]]*${token}[[:space:]]*$" "$file" 2>/dev/null || true) if [ -z "$found_lines" ]; then # Try exact match as fallback found_lines=$(grep -n "^#*${token}$" "$file" 2>/dev/null || true) fi if [ -z "$found_lines" ]; then echo "Warning: Token '$token' not found in $file_type file" return fi # Process each matching line (usually just one) while IFS= read -r line_info; do local line_num=$(echo "$line_info" | cut -d: -f1) local line_content=$(echo "$line_info" | cut -d: -f2-) # Check current status if [[ "$line_content" =~ ^[[:space:]]*#.* ]]; then local item_enabled=false else local item_enabled=true fi # Sync status if needed if [ "$macro_enabled" != "$item_enabled" ]; then if [ "$macro_enabled" = "true" ]; then # Enable: remove the # comment sed -i "${line_num}s/^[[:space:]]*#[[:space:]]*//" "$file" echo "Enabled: $token in $file_type ($line_num)" else # Disable: add # comment sed -i "${line_num}s/^[[:space:]]*/# /" "$file" echo "Disabled: $token in $file_type ($line_num)" fi changes_made=true else echo "Already synchronized: $token in $file_type" fi done <<< "$found_lines" } # Function to find and check/set status of items in js_list files # Processes fixed pair of tokens: first for str.js_list, second for help.js_list process_token_mapping() { local macro="$1" local macro_enabled="$2" local string_token="$3" local help_token="$4" # Process string token (first token - goes to str.js_list) process_single_token "$string_token" "$STR_LIST_FILE" "$macro_enabled" # Process help token (second token - goes to help.js_list) process_single_token "$help_token" "$HELP_LIST_FILE" "$macro_enabled" } echo "Starting synchronization..." changes_made=false # Process config map file line by line while IFS= read -r line; do # Skip empty lines and comments [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue # Parse line: MACRO string_token help_token read -ra parts <<< "$line" if [ ${#parts[@]} -ne 3 ]; then echo "Warning: Invalid mapping line format. Expected: MACRO string_token help_token. Got: $line" continue fi macro="${parts[0]}" string_token="${parts[1]}" help_token="${parts[2]}" # Check if macro exists in config file is_macro_enabled "$macro" macro_check_result=$? if [ $macro_check_result -eq 2 ]; then echo "Warning: Macro '$macro' not found in config file, skipping..." continue elif [ $macro_check_result -eq 0 ]; then macro_enabled=true else macro_enabled=false fi echo "Processing: $macro (enabled: $macro_enabled) -> str: $string_token, help: $help_token" # Process the pair of tokens for this macro process_token_mapping "$macro" "$macro_enabled" "$string_token" "$help_token" done < "$CONFIG_MAP_FILE" echo "----------------------------------------" if [ "$changes_made" = true ]; then echo " Synchronization completed with changes made" else echo " Synchronization completed - all files already in sync" fi exit 0 现在这个脚本的匹配功能逻辑是怎样的
最新发布
11-20
process_single_token() { local token="$1" local file="$2" local macro_enabled="$3" # Skip if token is empty if [ -z "$token" ]; then return fi # Determine file type for logging based on file path local file_type if [[ "$file" == *"str.js_list"* ]]; then file_type="str" else file_type="help" fi # Find lines containing this token (exact match) local found_lines=$(grep -n "^[[:space:]]*${token}[[:space:]]*$" "$file" 2>/dev/null || true) if [ -z "$found_lines" ]; then # Try exact match as fallback found_lines=$(grep -n "^#[[:space:]]*${token}$" "$file" 2>/dev/null || true) fi if [ -z "$found_lines" ]; then echo "Warning: Token '$token' not found in $file_type file" return fi # Process each matching line (usually just one) while IFS= read -r line_info; do local line_num=$(echo "$line_info" | cut -d: -f1) local line_content=$(echo "$line_info" | cut -d: -f2-) # Check current status if [[ "$line_content" =~ ^[[:space:]]*#.* ]]; then local item_enabled=false else local item_enabled=true fi # Sync status if needed if [ "$macro_enabled" != "$item_enabled" ]; then if [ "$macro_enabled" = "true" ]; then # Enable: remove the # comment sed -i "${line_num}s/^[[:space:]]*#[[:space:]]*//" "$file" echo "Enabled: $token in $file_type ($line_num)" else # Disable: add # comment sed -i "${line_num}s/^[[:space:]]*/# /" "$file" echo "Disabled: $token in $file_type ($line_num)" fi changes_made=true else echo "Already synchronized: $token in $file_type" fi done <<< "$found_lines" }这里会匹配多行吗
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值