Javascript 更改JSON结构
我需要更改JSON结构,但我正在努力如何做到这一点,而且我也不确定我是否需要创建一个新对象,或者我可以只处理当前对象? 无论如何,这是我要更改的JSON:Javascript 更改JSON结构,javascript,json,Javascript,Json,我需要更改JSON结构,但我正在努力如何做到这一点,而且我也不确定我是否需要创建一个新对象,或者我可以只处理当前对象? 无论如何,这是我要更改的JSON: [ {"document_name":"invoice_document.pdf"}, {"Invoice Number":"18021573"} ] 到 收益率: { “文件名称”:“发票文件.pdf”, “发票编号”:“18021573” }“您能给我们举一个例子,说明第一个数组中有多个对象时转换的效果吗?太好了,谢谢!
[
{"document_name":"invoice_document.pdf"},
{"Invoice Number":"18021573"}
]
到
收益率:
{
“文件名称”:“发票文件.pdf”,
“发票编号”:“18021573”
}“
您能给我们举一个例子,说明第一个数组中有多个对象时转换的效果吗?太好了,谢谢!
[
{
"document_name":"invoice_document.pdf",
"Invoice Number":"18021573"
}
]
let a = [
{"document_name":"invoice_document.pdf"},
{"Invoice Number":"18021573"}
];
// Use reduce on `a` because we know we want 1 after this is done.
// `Acc` is short for accumulator.
a = a.reduce((acc, i) => {
// Use Object.keys to get access to the key names.
Object.keys(i).map((key) => {
// Append item onto the accumulator using its key and value
// Warning: this will overwrite any existing `acc[key]` entries of the
// same `key` value.
acc[key] = i[key];
})
return acc;
// This last Object here is what we start with when reduce runs the first time.
}, {});