【发布时间】:2019-05-15 14:58:50
【问题描述】:
我想将 flat-Json 转换为分层, 或者我先将 excel 转换为 Json,然后尝试创建 JSON 对象层次结构。 我想将 flat-Json 转换为第 n 级的分层 json 对象。
Is there any middleware that conver Flat JSON to hirerachy JSON object.
{ "Name": "SAM", "Department": "CO", "Year": "2018"},
{ "Name": "SAM", "Address": "Mumbai", "Type": "Permanent"},
{ "Name": "John", "Department": "CE", "Year": "2018" },
{ "Name": "John", "Address": "Delhi", "Type": "Permanent" }
Expected Out-Put:
[{
"Name" : "SAM",
"Department": "CO",
"Year": "2018"
"children" : [{
"Name": "SAM",
"Address": "Mumbai",
"Type": "Permanent"
}]
},
{
"Name" : "John",
"Department": "CE",
"Year": "2018"
"children" : [{
"Name": "John",
"Address": "Delhi",
"Type": "Permanent"
}]
}]
【问题讨论】:
-
你真的想要一个外部库来做这个吗?
-
到目前为止你尝试过什么?提示:这是一个非常简单但效率低下的
Array.reduce()循环。添加 children 属性,过滤所有这些子元素的原始数组,将它们添加到属性中,重复。具有讽刺意味的是,原始数组比嵌套后更容易使用。 -
@FedericoklezCulloca 谢谢 .. 是的,我想要像 npm xls-to-json 这样的外部包
标签: javascript npm dom-events npm-scripts