遍历对象

  //forin循环
  
  let objs = [{
          id: 1,
          name: 'object1'
        }, {
          id: 2,
          name: 'object2'
        }, {
          id: 3,
          name: 'object3'
     }]
     
    for (var obj in objs) {
        console.log('name ')
        console.log(objs[obj]['name'])
        console.log('obj')
        console.log(objs[obj])
        console.log('对象数组中地址')
        console.log(obj)
      }

遍历数组

//forin循环

 let arrys = ['张三', '李四', '王五']
 
    for (var arry in arrys) {
      console.log('数组内容')
      console.log(arrys[arry])
      console.log('数组下标')
      console.log(arry)
  }

遍历集合

 //for循环

		let obj = {
	       id: 1,
	         name: 'object1'
	    }
        
        let obj1 = {
          id: 2,
          name: 'object2'
        }
        
        let lists = []
        lists.push(obj)
        lists.push(obj1)
        
     for (var i=0;i<=lists.length;i++){
        console.log("内容");
        console.log(lists[i]);
        console.log("集合对象属性");
        console.log(lists[i]['name']);
        console.log("集合下标");
        console.log(i);
      }

遍历字典 (Map)

 //forin循环
  
     let dics  = {
          b: 2,c: 3, a: 1
     }
        
      for(var key in dics){
        console.log("key: "+key+" value: "+dics[key])
      }
      
      console.log("字典元素按key值排序: ");
      var res = Object.keys(dics).sort();
      
      for(var key in res){
        console.log("key: " + res[key] + " ,value: " + dics[res[key]]);
      }

      console.log("字典元素按value值排序: ");
      var res2 = Object.keys(dics).sort(function(a,b){ return dics[a]-dics[b]; });
      
      for(var key in res2){
        console.log("key: " + res2[key] + " ,value: " + dics[res2[key]]);
      }
Logo

前往低代码交流专区

更多推荐