gpt4 book ai didi

javascript - 格式化扩展的 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 12:16:39 25 4
gpt4 key购买 nike

我有一个类似这样的 JSON 对象。与 JSON.stringify(Object) 一起使用时,它看起来像这样。

 "[
["Individual Id","Individual Last Name","Individual First Name","Individual Middle Name"],
["1268933","mrs","jeanine","marie"]
["1268934","mr","jasmine","curry"]
]"

将数据重新格式化为以下格式的最快方法是什么。

[
{
"Individual Id": "1268933",
"Individual Last Name": "mrs",
"Individual First Name": "jeanine",
"Individual Middle Name": "marie"
},
{
"Individual Id": "1268934",
"Individual Last Name": "miss",
"Individual First Name": "jasmine",
"Individual Middle Name": "curry"
}
]

最佳答案

您可以array#shift第一个数组,然后使用array#maparray#reduce生成数组。

var arr = [["Individual Id","Individual Last Name","Individual First Name","Individual Middle Name"],["1268933","mrs","jeanine","marie"],["1268934","mr","jasmine","curry"]],
headers = arr.shift(),
result = arr.map(a => a.reduce((r,v,i) => (r[headers[i]] = v, r), {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 格式化扩展的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48111615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com