vue循环迭代对象、数组和函数
原创vue 循环对象、数组和字符串。
1.循环遍历对象
1.1vue 在html循环内遍历对象。
v-for=" (val, key , i) in dimItemMap" :key="key"
val-每一项
key -key值
i-第几个
            
              
                
                  {{val.score}}//score为键,val.score为值
                
           
       
 1.2 在js里面forin遍历对象
for…in 循环主要用于遍历对象,而不是遍历数组。
let data = [{ wiun2dvi: 232, wiun3dvi: 55, wiu4ndvi: 33, wiun1dvi: 24432 }];
    data.forEach((item,index)=>{
      for (const key in item) {
        console.log("item[key]",item[key]);//值
        console.log("key",key);//键
      }
    })2.循环遍历数组
2.1 vue 在html在循环内部遍历数组。
      
           {{scope.row.dimItemMap?scope.row.dimItemMap[item.id].name:""}}
      
 
            
              
                
                  {{item.score}}
                
           
       
 2.2 在js里面for遍历数组
let id = 1524466
for (let i = 0; i < list.length; i++) {
        let a = list[i];
        if (a.id === id) {
          return a.name;
        } 
}2.3 在js里面forof遍历数组
           let arr = [{
                name: 12,
                hello: "wsdfnioq",
            }, {
                name: 12,
                hello: "wsdfnioq",
            }, {
                name: 12,
                hello: "wsdfnioq",
            }]
            for (const i of arr) {
                console.log("i", i);
            }
//i {name: 12, hello: wsdfnioq}
// i {name: 12, hello: wsdfnioq}
 //i {name: 12, hello: wsdfnioq}
let arr = [
    [name, "张三"],
    [地址, 北京],
    [手机, 123]
]
for (const [value0, value1] of arr) {
    console.log(k, value0, value1);
}
// k name 张三
// k 地址 北京
// k 手机 1232.4 forin,不建议遍历数组
let arr = ["lsadnkol", "klsmvaod", "lpsaojfoas"]
for (const key in arr) {
    console.log("arr", key, typeof key, arr[key]);
}
// 0 string lsadnkol
// 1 string klsmvaod
// 2 string lpsaojfoas2.4 forEach() 函数遍历数组
(1) 不返回,可以更改原始数组的内容
(2) 循环数:数组长度
③不支持return,不需要return语句
以下情况:添加到每个对象age属性
    let forArr = [{name:tom,sex:man},{name:linda,sex:woman},]
     forArr.forEach((item,index) => {
        console.log("forEach循环==index==",index,item);
         item.age = 27
    })
    console.log("forArr==遍历后===",forArr)
   // forArr ---->[{name:tom,sex:man,age:27},{name:linda,sex:woman,age:27}]3.循环遍历字符串
3.1for
let s = "abcd"
for (let i = 0; i < s.length; i++) {
      console.log(i, typeof i, s[i]); //i为索引,s[i]为值,typeof i 为number
}
//  0 number a
//  1 number b
//  2 number c
//  3 number d3.2 forin
let s = "abcd"
for (const key in s) {
    console.log("key", key, typeof key, s[key]); //key为索引,s[key]为值,typeof key 为string
}
// 0 string a
// 1 string b
// 2 string c
// 3 string d3.3 forof
let s = "abcd"
for (const i of s) {
    console.log("i", i);//i为值
}
// a 
// b 
// c 
// d版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
 itfan123
itfan123







