【问题标题】:Converting and formating JSON objects in Javascript在 Javascript 中转换和格式化 JSON 对象
【发布时间】:2020-11-22 16:23:55
【问题描述】:

我有这个 JSON 对象:

{
"date": "01/01/2020",
"name": "New Year",
"type": "International Holiday",
"description": "",
"type_code": "1"
}

我需要用这种模式创建另一个 JSON 对象:

{
   "01/01/2020":{
      "selected":true,
      "marked":true,
      "selectedColor":"green"
   }
}

现在,我只需要将 date 字段中的值作为新对象的键名。 有什么建议吗?

【问题讨论】:

    标签: javascript json react-native


    【解决方案1】:

    试试这个方法

    const object = {
    
          "date": "01/01/2020",
          "name": "New Year",
          "type": "International Holiday",
          "description": "",
          "type_code": "1"
    };
    
    
    const newObject = {};
    
    newObject[object.date] = {
          "selected":true,
          "marked":true,
          "selectedColor":"green"
    };
    
    console.log(newObject); // result
    

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      相关资源
      最近更新 更多