[英]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.