代码之家  ›  专栏  ›  技术社区  ›  William

交换JSON键和值

  •  1
  • William  · 技术社区  · 7 年前

    在创建对象之前,我是否可以反转键和值?我试图用下划线来实现这一点,但这个库只允许反转对象。

    <!DOCTYPE html>
    <html>
       <body>
       <script src="http://underscorejs.org/underscore-min.js"></script>
          <script>
             var myObj = {
                    "503": "07:25",
                     "507": "06:00",
                     "500x": "06:50",
                     "500x": "07:20",
                     "500": "07:35",
                     "503": "07:50",
                     "507": "07:40",
                     "500x": "07:55",
                     "500": "08:30",
                     "500x": "08:00",
                     "500": "10:45",
                     "507": "09:05",
                     "500": "10:45",
                     "507": "09:05",
                     "500": "13:45",
                     "500": "16:45",
                     "500": "20:00",
                     "500": "22:00",
                     "500N\n*Thur/Fri Only": "23:00"
           },
    
           myObj = _.invert(myObj),
           keys = Object.keys(myObj),
           values = Object.values(myObj),
           i, len = values.length;
           console.log("Total len = " + len) 
           values.sort();
    
           console.log(myObj);
    
         for (i = 0; i < len; i++) {
           k = keys[i];
           v = values[i];
           console.log(k + ': ' + v);
         }
          </script>
       </body>
    </html>
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   Hassan Imam Ravi kant    7 年前

    如果您可以将obj转换为字符串,那么使用正则表达式可以得到 ""

    \n 内部字符串。

    const myObj = '{"503": "07:25","507": "06:00","500x": "06:50","500x": "07:20","500": "07:35","503": "07:50","507": "07:40","500x": "07:55","500": "08:30","500x": "08:00","500": "10:45","507": "09:05","500": "10:45","507": "09:05","500": "13:45","500": "16:45","500": "20:00","500": "22:00", "500N\\n*Thur/Fri Only" : "23:00"}';
           
    var result = myObj.match(/"(.*?)"/gm).reduce((res,word)=>{
      res.push(word.replace(/"/g,''));
      return res;
    },[]);
    
    var swapped = {};
    for(let i = 0 ; i < result.length - 1; i += 2){
      swapped[result[i + 1]] =result[i]
    }
    
    console.log(swapped);
        2
  •  0
  •   Chandra Kumar    7 年前

    使用此代码:

    var myObj = {
     "503": "07:25",
     "507": "06:00",
     "500x": "06:50",
     "500x": "07:20",
     "500": "07:35",
     "503": "07:50",
     "507": "07:40",
     "500x": "07:55",
     "500": "08:30",
     "500x": "08:00",
     "500": "10:45",
     "507": "09:05",
     "500": "10:45",
     "507": "09:05",
     "500": "13:45",
     "500": "16:45",
     "500": "20:00",
     "500": "22:00",
     "500N\n*Thur/Fri Only": "23:00"
    };
    
    function swap(json){
    var ret = {};
    for(var key in json){
    ret[json[key]] = key;
    }
    return ret;
    }
    
    console.log(myObj);
    console.log(swap(myObj));
    <!DOCTYPE html>
    <html>
       <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
       <script src="http://underscorejs.org/underscore-min.js"></script>
       </body>
    </html>