简体   繁体   English

在 Javascript 中转换和格式化 JSON 对象

[英]Converting and formating JSON objects in Javascript

I Have this JSON object:我有这个 JSON 对象:

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

I need to create another JSON object with this pattern:我需要使用此模式创建另一个 JSON 对象:

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

For now, I just need the value in date field to be the key's name of the new object.现在,我只需要date字段中的值作为新对象的键名。 Any sugestions?有什么建议吗?

Try this way试试这个方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM