zoukankan      html  css  js  c++  java
  • 数组,对象遍历

    一.for-in

     1.遍历数组:
       for(item in array) { 
            console.info(array[item])
         }
    item =>下标
    2.遍历对象
    for(item in obj) { 
            console.info(item);
          }
    item =>属性名
    二.forEach
    1.遍历数组
    array.forEach(function(v, i, a) {
            console.log(i);
      });
    v =>数组的每一项
    i =>下标
    a =>数组本身
     一个参数:
    array1.forEach(function(key) {
            console.log(key);
          });
    key =>数组的每一项内容
    三.for-of
    1.遍历数组
    for(let item of array) {
            console.info(item.name)
          }
    item =>数组的每一项
    2.遍历字符串,(替换,查找)
    for(item of st) {
            console.info(item)
          }
    item =>字符串的每一个拆分值
    3.部署了 Iterator 接口的对象
        const map = new Map();
        map.set('first', 'hello');
        map.set('second', 'world');
        for(let keys of map){
          console.info(keys)
        }
    key => 每一项键值对,以数组形式返回:["first","hellow"]
    四.json转换
         var jsonString ='{"1":"909","2":"409"}'
          var jsonObj1 =JSON.parse(jsonString)
          // 这种方式不安全,eval会执行json串中的表达式
          var jsonObj = eval('(' + jsonString + ')')
          var toString = JSON.stringify(jsonObj)
          console.info(jsonObj1)
          console.info(jsonObj)
          console.info(toString)
    五.概念
    1.json串 var jsonString ='{"1":"909","2":"409"}'
    2.json对象 var jsonString ={"1":"909","2":"409"}
    3.json数组对象 var jsonString =[{"1":"909","2":"409"},{"1":"909","2":"409"}]
  • 相关阅读:
    【leetcode】1442. Count Triplets That Can Form Two Arrays of Equal XOR
    【leetcode】1441. Build an Array With Stack Operations
    【leetcode】1437. Check If All 1's Are at Least Length K Places Away
    cxCheckCombobox
    修改现有字段默认值
    2018.01.02 exprottoexcel
    Statusbar OwnerDraw
    dxComponentPrinter记录
    单据暂存操作思路整理
    设置模式9(装饰者,责任链,桥接,访问者)
  • 原文地址:https://www.cnblogs.com/LWJ-booke/p/8487030.html
Copyright © 2011-2022 走看看