【问题标题】:how to find if key value pair exists in json object using switch statement如何使用switch语句查找json对象中是否存在键值对
【发布时间】:2021-01-12 19:42:11
【问题描述】:
json type :1
var x={
    "address": {
        "county": "abc",
        "state_district": "asd",
        "state": "test",
        "country": "test1",
        "country_code": "test"
    }
 }
Type:2
var x ={ "address": {
            "suburb": "",
            "city_district": "",
            "city": "",
            "county": "",
            "state": "",
            "postcode": "",
            "country": "",
           
        }}
type:3
 var x= {"address": {
            "amenity": "",
            "road": "",
            "town": "",
            "county": "",
            "state_district": "",
            "state": "",
            "postcode": "",
            "country": "",
            "country_code": ""
        }
    }
switch(x.address)
{
    case 'city':
        return x.address.city;
        break;
    case 'village':
        return x.address.village;
        break;    
    default:
        alert('err');}

这是我的 JSON 内容。 address 属性将具有一些额外的键值,具体取决于位置,例如 cityvillagesuburb 等。

我需要使用 switch 语句检查特定键是否存在。上面显示了带有附加属性的示例 JSON。

【问题讨论】:

    标签: javascript json switch-statement


    【解决方案1】:

    除了Fiouze的回答,你还可以这样做:

    // will return true if the property exists.
    'city' in x.address
    
    // will return a value if set or undefined if not.
    x.address.city
    

    但是,如果您想坚持使用 switch case,您可以通过执行以下操作来遍历属性:

    for (const prop in x.address) {
      switch (prop) {
        case 'city': 
          return x.address.city;
        case 'village':
          return x.address.village;
        default:
          alert('err');
      }
    }
    

    参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

    【讨论】:

      【解决方案2】:

      您也许可以使用Object.prototype.hasOwnProperty()

          if(x.address.hasOwnProperty('city')){
              return x.address.city
          }else if(x.address.hasOwnProperty('village')){
              return(x.address.village)
          }else{
              alert('err')
          }
      
      See https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/hasOwnProperty
      

      【讨论】:

        【解决方案3】:

        Switch 旨在评估单个值,而不是对象的多个属性名称。

        以下函数 fn() 复制了您的 switch 块的意图:

        const myObject = {
          "address": {
            "amenity": "",
            "road": "",
            "town": "FAKE_TOWN",
            "county": "",
            "state_district": "",
            "state": "",
            "postcode": "",
            "country": "",
            "country_code": ""
          }
        };
        
        function fn(obj) {
          const {address} = obj;
          
          // get value of the first property that exists
          // note: `??` is the nullish coalescing operator
          const val =
            address.city ??
            address.village ??
            address.town;
        
          // duplicate the `default` case behavior
          if (val === undefined) {
            alert('err');
            return undefined;
          }
        
          return val;
        }
        
        console.log(`fn: "${fn(myObject)}"`);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-15
          • 1970-01-01
          • 1970-01-01
          • 2020-07-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多