简体   繁体   English

使用 Javascript 我如何将这个对象数组解构和重建为 json 对象

[英]Using Javascript how would I deconstruct and reconstruct this array of objects to json objects

Basically how do I convert this:基本上我如何转换这个:

files = [
 { 
   transformation: 't_16x9_lg',
   width: 1920,
   height: 1080,
   bytes: 633634,
   format: 'jpg',
   url: 'xxx.png'
 },
 { 
   transformation: 't_16x9_md',
   width: 1440,
   height: 810,
   bytes: 383730,
   format: 'jpg',
    url: 'xxx.png'
 }
]

To this:对此:

files = { 
  't_16x9_lg': { 
    width: 1920,
    height: 1080,
    bytes: 633634,
    format: 'jpg',
    url: 'xxx.png'
  },
  't_16x9_md': {
    width: 1440,
    height: 810,
    bytes: 383730,
    format: 'jpg',
    url: 'xxx.png'
  }
}

Stack overflow is asking me to add more details but not sure what else to add here.堆栈溢出要求我添加更多详细信息,但不确定此处还要添加什么。 I guess just ask if you need more details.我想只是问你是否需要更多细节。

Thanks.谢谢。

You could destructure transformation and build new objects.您可以解构transformation并构建新对象。

 var files = [{ transformation: 't_16x9_lg', width: 1920, height: 1080, bytes: 633634, format: 'jpg', url: 'xxx.png' }, { transformation: 't_16x9_md', width: 1440, height: 810, bytes: 383730, format: 'jpg', url: 'xxx.png' }], result = Object.fromEntries( files.map(({ transformation, ...object }) => [transformation, object]) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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

相关问题 如何使用JQuery / JavaScript将对象数组中的值分组并从这些组中创建新的对象数组 - Using JQuery/JavaScript how would I group values in an array of objects and create a new array of objects from the groups 如何将这个对象的对象的对象解构为对象的对象? - How do I deconstruct this object of objects of objects into object of objects? 在包含嵌套数组的javascript中重建对象数组 - Reconstruct an array of objects in javascript that contains nested arrays Scala像javascript对象一样解构 - Scala deconstruct like javascript objects 如何使用Javascript或Jquery将JSON对象包装在数组中? - How can I wrap JSON objects in an array using Javascript or Jquery? (nodejs,javascript)如何使用变量在JSON文件中查找对象? - (nodejs, javascript) How would I go about using a variable to find objects in a JSON file? 在 Javascript 中重建 JSON 数组 - Reconstruct JSON Array in Javascript javascript - 使用 for (key in json) - 我想得到所有其他关键注释 json 只包含对象 - javascript - using for (key in json) - I would like to get every other key note json consists only of objects 如何遍历 Javascript 中的 JSON 对象数组? - How do I iterate through an array of JSON objects in Javascript? 如何在邮递员中使用JavaScript向JSON数组添加唯一对象 - How to add unique objects to json array using javascript in postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM