【问题标题】:Is there any "middleware" or npm package Convert flat JSON file to hierarchical json data? [closed]是否有任何“中间件”或 npm 包将平面 JSON 文件转换为分层 json 数据? [关闭]
【发布时间】: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


【解决方案1】:

这是你正在寻找的功能

var arry = [{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" },
           { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" },
           { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" },
           { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }];
function convert(array){
  var map = {};
  for(var i = 0; i < array.length; i++){
    var obj = array[i];
    obj.items= [];

    map[obj.Id] = obj;

    var parent = obj.Parent || '-';
    if(!map[parent]){
        map[parent] = {
            items: []
        };
    }
    map[parent].items.push(obj);
  }
  return map['-'].items;
}

var r = convert(arry);

结果

[{
    "Id" : "1",
    "Name" : "abc",
    "Parent" : "",
    "attr" : "abc",
    "children" : [{
                "Id" : "2",
                "Name" : "abc",
                "Parent" : "1",
                "attr" : "abc",
                "children" : [{
                            "Id" : "3",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }, {
                            "Id" : "4",
                            "Name" : "abc",
                            "Parent" : "2",
                            "attr" : "abc",
                            "children" : []
                        }]
            }]
}]

复制自https://stackoverflow.com/a/15376430/1826429

【讨论】:

  • 谢谢 .. 你是对的,但我想要 npm 包或中间件,所以我可以将我的 json 对象传递给中间件并获得预期的结果,这将清理或减少我的代码。喜欢 npm xls-to-json 非常感谢...
  • 是否有任何中间件可以将 Flat JSON 转换为 hierachy JSON 对象。
猜你喜欢
  • 2013-10-19
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2019-06-20
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
相关资源
最近更新 更多