【问题标题】:Json using hierarchial tree using javascriptJson 使用 javascript 使用层次结构树
【发布时间】:2017-09-14 19:08:18
【问题描述】:

我有一个 JSON 数组。我尝试转换嵌套分层树。但它没有用。我需要一个使用这个 JSON 的层次树。

这是我的数据:

[{
    "_id" : "59b65ee33af7a11a3e3486c2",
    "C_TITLE" : "Sweet and Snacks",
    "C_PARENT" : "0",
    "C_ICON" : "",
    "C_IMAGE" : "59b65ee33af7a11a3e3486c2_sweets.jpg",
    "C_STATUS" : "Active"
}
{
    "_id" : "59b663d709da571dc3d79f49",
    "C_TITLE" : "Groceries",
    "C_PARENT" : "0",
    "C_ICON" : "",
    "C_IMAGE" : "59b663d709da571dc3d79f49_grocery.jpg",
    "C_STATUS" : "Active"
},
{
    "_id" : "59b6648209da571dc3d79f4a",
    "C_TITLE" : "Dals & Pulses",
    "C_PARENT" : "59b663d709da571dc3d79f49",
    "C_ICON" : "",
    "C_IMAGE" : "59b6648209da571dc3d79f4a_dals.jpg",
    "C_STATUS" : "Active"
},
{
    "_id" : "59b6657509da571dc3d79f4c",
    "C_TITLE" : "Rice & Rice products",
    "C_PARENT" : "59b663d709da571dc3d79f49",
    "C_ICON" : "",
    "C_IMAGE" : "59b6657509da571dc3d79f4c_rice.jpg",
    "C_STATUS" : "Active"
}]

我需要这样的输出

[
    {
        " _id" : "59b65ee33af7a11a3e3486c2",
        "C_TITLE" : "Sweet and Snacks",
        "C_PARENT" : "0",
        "C_ICON" : "",
        "C_IMAGE" : "59b65ee33af7a11a3e3486c2_sweets.jpg",
        "C_STATUS" : "Active",
        children:[]


    }
   ]

基于检查 C_PARENT 和 _id。

【问题讨论】:

  • 你能添加你预期的输出吗
  • [ { "_id" : "59b65ee33af7a11a3e3486c2", "C_TITLE" : "糖果和零食", "C_PARENT" : "0", "C_ICON" : "", "C_IMAGE" : "59b65ee33af7a11a3e3486c2_sweets。 jpg”、“C_STATUS”:“活动”、儿童:{“_id”:“ghhgfhfhhhg”、“C_TITLE”:“糖果和零食”、“C_PARENT”:“59b65ee33af7a11a3e3486c2”、“C_ICON”:“”、“C_IMAGE” : "59b65ee33af7a11a3e3486c2_sweets.jpg", "C_STATUS" : "活动", } } ]
  • 感谢您的回复。我需要以上输出。
  • 您应该更新问题中的预期输出,最好让我们看到@sarankani

标签: javascript json


【解决方案1】:

基本上,你需要代替

o[a.id]

这个

o[a._id]

因为您的数据有 _id 作为密钥 ID。

另一个问题是与root 的严格比较,你需要一个字符串作为值'0',因为这是你拥有的值。

var data = [{ _id: "59b65ee33af7a11a3e3486c2", C_TITLE: "Sweet and Snacks", C_PARENT: "0", C_ICON: "", C_IMAGE: "59b65ee33af7a11a3e3486c2_sweets.jpg", C_STATUS: "Active" }, { _id: "59b663d709da571dc3d79f49", C_TITLE: "Groceries", C_PARENT: "0", C_ICON: "", C_IMAGE: "59b663d709da571dc3d79f49_grocery.jpg", C_STATUS: "Active" }, { _id: "59b6648209da571dc3d79f4a", C_TITLE: "Dals & Pulses", C_PARENT: "59b663d709da571dc3d79f49", C_ICON: "", C_IMAGE: "59b6648209da571dc3d79f4a_dals.jpg", C_STATUS: "Active" }, { _id: "59b6657509da571dc3d79f4c", C_TITLE: "Rice & Rice products", C_PARENT: "59b663d709da571dc3d79f49", C_ICON: "", C_IMAGE: "59b6657509da571dc3d79f4c_rice.jpg", C_STATUS: "Active" }],
    result = function (array, root) {
        var r = [], o = {};
        array.forEach(function (a) {
            a.children = o[a._id] && o[a._id].children; // change to a._id
            o[a._id] = a;                               // change to a._id
            if (a.C_PARENT === root) {                  // strict comparison!
                r.push(a);
                return;
            }
            o[a.C_PARENT] = o[a.C_PARENT] || {};
            o[a.C_PARENT].children = o[a.C_PARENT].children || [];
            o[a.C_PARENT].children.push(a);
        });
        return r;
    }(data, "0");                                       // "0" as root

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 我怀疑这是否适用于未排序的多级树。
  • @bluehipy,它适用于任何排序的原始数据,因为对象 o 保留了对节点的所有引用。
  • @bluehipy,你可以看看here,该功能是如何工作的。
  • 我知道它是如何工作的......您正在使用o 通过 id 来“记住”所有节点并推测对象的 javascript 静态引用,以便您推入 @987654332 的任何内容@ 反映在输出树中。我只是想指出,初始数据必须按 id 进行排序,其方式是必须在子级之前声明父级。
  • 不,数据不必提前排序,你可以尝试不同的排序。
猜你喜欢
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 2018-02-21
  • 2015-12-05
  • 2023-03-21
  • 2018-10-01
相关资源
最近更新 更多