zoukankan      html  css  js  c++  java
  • js判断对象的某个属性是否存在

    参考:https://www.jb51.net/article/141994.htm

    原始数据,

    [
    
        {"name":"向阳镇","id":"10","pId":0,"open": "true"},
    
        {"name":"彩蓝桥镇","id":"20","pId":0,"open": "true"},
    
        {"name":"丁向镇","id":"30","pId":0,"open": "true"},  
    
        {"name":"桃园东","id":"1","lng":"120.56934479","lat":31.85618831,"region":"向阳河","pId":10,"waterQuality":"I"},
    
        {"name":"董家桥村","id":"2","lng":"120.56709498","lat":31.90134260,"region":"向阳河","pId":10,"waterQuality":"II"},
    
        {"name":"庄上村","id":"3","lng":"120.55362585","lat":31.87984724,"region":"彩蓝桥河","pId":20,"waterQuality":"III"},
    
        {"name":"绣品街","id":"4","lng":"120.53118860","lat":31.86526444,"region":"彩蓝桥河","pId":20,"waterQuality":"IV"},
    
        {"name":"下村","id":"5","lng":"120.57111579","lat":31.85730531,"region":"丁向河","pId":30,"waterQuality":"V"}
    
    ]
    

     处理过程,给不存在的属性赋值,赋值之后,该对象不存在的属性就存在了

    var getData = response.data;
          for(let i=0;i<getData.length;i++){
             if(getData[i].open == undefined){ getData[i].open =""; }
             if(getData[i].lng == undefined){ getData[i].lng =""; }
             if(getData[i].lat == undefined){ getData[i].lat =""; }
             if(getData[i].region == undefined){ getData[i].region =""; }
             if(getData[i].waterQuality == undefined){ getData[i].waterQuality=""; }
          }
    

     处理之后数据

    [
        {"name":"向阳镇",  "id":"10", "pId":0,  "open": "true",   "lng":"",            "lat":"",         "region":"",        "waterQuality":""},
        {"name":"彩蓝桥镇","id":"20", "pId":0,  "open": "true",   "lng":"",            "lat":"",         "region":"",        "waterQuality":""},
        {"name":"丁向镇",  "id":"30", "pId":0,  "open": "true",   "lng":"",            "lat":"",         "region":"",        "waterQuality":""},  
        {"name":"桃园东",  "id":"1",  "pId":10, "open":"",         "lng":"120.56934479","lat":31.85618831,"region":"向阳河",  "waterQuality":"I"},
        {"name":"董家桥村","id":"2",  "pId":10, "open":"",         "lng":"120.56709498","lat":31.90134260,"region":"向阳河",  "waterQuality":"II"},
        {"name":"庄上村",  "id":"3",  "pId":20, "open":"",         "lng":"120.55362585","lat":31.87984724,"region":"彩蓝桥河","waterQuality":"III"},
        {"name":"绣品街",  "id":"4",  "pId":20, "open":"",         "lng":"120.53118860","lat":31.86526444,"region":"彩蓝桥河","waterQuality":"IV"},
        {"name":"下村",    "id":"5",  "pId":30, "open":"",         "lng":"120.57111579","lat":31.85730531,"region":"丁向河",  "waterQuality":"V"}
    ];
    

     解释:

    // 创建对象
     let test = {name : 'lei'} // 获取对象的自身的属性
     test.name   //"lei"
     test["name"]   //"lei"  
     // 获取不存在的属性
     test.age    //undefined 
    // 获取原型上的属性
     test["toString"]  //toString() { [native code] }   
    // 新增一个值为undefined的属性 
    test.un = undefined   
    test.un    //undefined 不能用在属性值存在,但可能为 undefined的场景 
    
  • 相关阅读:
    Scala集合
    Spark常用算子
    Flink运行架构
    Flink 有状态的算子和应用程序
    Flink 状态一致性
    Flink 检查点(checkpoint)
    Flink 时间语义与watermark
    Flume的可靠性保证:故障转移、负载均衡
    Hive 文件存储格式
    BPM与OA区别
  • 原文地址:https://www.cnblogs.com/yingyigongzi/p/10820728.html
Copyright © 2011-2022 走看看