当前位置:网站首页>JS dynamically generates variable names and assigns values

JS dynamically generates variable names and assigns values

2022-06-25 00:21:00 Mr, Guo

JS Dynamically generate variable names and assign values

let vars = {
    }; // Batch definition attribute name 
for(var i = 0;i < 10; i++){
    
    let attrName = 'my-'+i;  // Dynamically define variable names 
    vars[attrName] = 'value-'+i;  // Dynamic assignment 
}
console.log(vars);
## jquery obtain table Although there are rows of data and dynamic attribute names and values are generated 
let tableArr = []; // Save all data 
$("#funTable tr:not(:first)").each(function(){
     // Traverse all rows except the header row 
    let vars={
    };  // Store row data 
    $("input:not(:button),select",this).each(function(){
     // Traversing in-line input select Value   Exclude button 
        let attrName = this.name;  // Dynamically define the attribute name 
        vars[attrName] = $(this).val();  // Dynamically assign values to attribute names 
    });
    tableArr.push(vars); // Row data format 
});
原网站

版权声明
本文为[Mr, Guo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210548307573.html