使用javascript将平面数组转换为树结构

问题描述 投票:0回答:3

我在转换平面数组时遇到问题,可以使用您的帮助!

当前数据格式是这样的:

    [
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": null,
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 1,
            "OVERALL_MARKET_VALUE": 8
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
            "MARKET_VALUE": 10,
            "OVERALL_MARKET_VALUE": 5
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
            "MARKET_VALUE": 5,
            "OVERALL_MARKET_VALUE": 3
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 5,
            "OVERALL_MARKET_VALUE": 10
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1
        },
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
            "MARKET_VALUE": 10,
            "OVERALL_MARKET_VALUE": 5
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": null,
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 1,
            "OVERALL_MARKET_VALUE": 8
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
            "MARKET_VALUE": 10,
            "OVERALL_MARKET_VALUE": 5
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
            "MARKET_VALUE": 5,
            "OVERALL_MARKET_VALUE": 3
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 5,
            "OVERALL_MARKET_VALUE": 10
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
            "MARKET_VALUE": 10,
            "OVERALL_MARKET_VALUE": 5
        }
    ]

我希望最终得到这样的结果:

    [
        {
            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
            "SUB_ACCOUNT_NAME": null,
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1,
            "children": [
                {
                    "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                    "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                    "TERTIARY_ACCOUNT_NAME": null,
                    "MARKET_VALUE": 1,
                    "OVERALL_MARKET_VALUE": 8,
                    "children": [
                        {
                            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
                            "MARKET_VALUE": 10,
                            "OVERALL_MARKET_VALUE": 5
                        },
                        {
                            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
                            "MARKET_VALUE": 5,
                            "OVERALL_MARKET_VALUE": 3
                        },
                    ]
                },
                {
                    "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                    "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                    "TERTIARY_ACCOUNT_NAME": null,
                    "MARKET_VALUE": 5,
                    "OVERALL_MARKET_VALUE": 10,
                    "children": [
                        {
                            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
                            "MARKET_VALUE": 6,
                            "OVERALL_MARKET_VALUE": 1
                        },
                        {
                            "MASTER_ACCOUNT_NAME": "MAIN_ENTITY",
                            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
                            "MARKET_VALUE": 10,
                            "OVERALL_MARKET_VALUE": 5
                        },
                    ]
                },
            ]
        },
        {
            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
            "SUB_ACCOUNT_NAME": null,
            "TERTIARY_ACCOUNT_NAME": null,
            "MARKET_VALUE": 6,
            "OVERALL_MARKET_VALUE": 1,
            "children": [
                {
                    "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                    "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                    "TERTIARY_ACCOUNT_NAME": null,
                    "MARKET_VALUE": 1,
                    "OVERALL_MARKET_VALUE": 8,
                    "children": [
                        {
                            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
                            "MARKET_VALUE": 10,
                            "OVERALL_MARKET_VALUE": 5
                        },
                        {
                            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                            "SUB_ACCOUNT_NAME": "MAIN_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
                            "MARKET_VALUE": 5,
                            "OVERALL_MARKET_VALUE": 3
                        },
                    ]
                },
                {
                    "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                    "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                    "TERTIARY_ACCOUNT_NAME": null,
                    "MARKET_VALUE": 5,
                    "OVERALL_MARKET_VALUE": 10,
                    "children": [
                        {
                            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_1",
                            "MARKET_VALUE": 6,
                            "OVERALL_MARKET_VALUE": 5
                        },
                        {
                            "MASTER_ACCOUNT_NAME": "SECOND_ENTITY",
                            "SUB_ACCOUNT_NAME": "SECOND_ACCOUNT",
                            "TERTIARY_ACCOUNT_NAME": "TERTIARY_ACCOUNT_2",
                            "MARKET_VALUE": 10,
                            "OVERALL_MARKET_VALUE": 5
                        },
                    ]
                },
            ]
        },
    ]

很抱歉的尺寸!顶层部分为MASTER_ACCOUNT_NAME,其后部分为SUB_ACCOUNT_NAME,最低部分为TERTIARY_ACCOUNT_NAME。

我已经尝试过使用lodash和vanilla javascript来做到这一点,但是不得不承认,数据操作并不是我真正的技能领域,我正在为此而苦苦挣扎!

谢谢您能为我提供的任何帮助!

javascript arrays treeview
3个回答
0
投票

此解决方案特定于您的示例数据,如果您具有其他数据形状,则可能需要对其进行调整。

但是,如果您有1000件物品,不建议使用。

function makeTree(data) {
  return data
    .filter(
      (masterItem) =>
        masterItem.SUB_ACCOUNT_NAME === null &&
        masterItem.TERTIARY_ACCOUNT_NAME === null
    )
    .map((masterItem) => ({
      ...masterItem,
      children: data
        .filter(
          (subItem) =>
            subItem.MASTER_ACCOUNT_NAME === masterItem.MASTER_ACCOUNT_NAME &&
            subItem.SUB_ACCOUNT_NAME !== null &&
            subItem.TERTIARY_ACCOUNT_NAME === null
        )
        .map((subItem) => ({
          ...subItem,
          children: data.filter(
            (tertiaryItem) =>
              tertiaryItem.SUB_ACCOUNT_NAME === subItem.SUB_ACCOUNT_NAME &&
              tertiaryItem.TERTIARY_ACCOUNT_NAME !== null
          ),
        })),
    }));
}

0
投票

如何使用javascript和lambdas来操纵数组?像这样的东西:

const yourArray = [....];
const result = yourArray.filter(obj => obj['SUB_ACCOUNT_NAME'] == null);
result.forEach(node => node["CHILDRENS"] = yourArray.filter(obj => obj['SUB_ACCOUNT_NAME'] != null && obj['TERTIARY_ACCOUNT_NAME'] == null && obj["MASTER_ACCOUNT_NAME"] == node["MASTER_ACCOUNT_NAME"]));

这将创建一个二级结构。现在,要生成第三个结构,您将需要对每个第二级节点执行类似的操作。可以使用一个函数对其进行概括。如下所示:

function putChildrenFromArray(nodes, elements, howToSelectSons) {
  nodes.forEach(node => node["CHILDRENS"] = elements.filter(obj => howToSelectSons(obj, node)));
}

这样称呼:

putChildrenFromArray(result, yourArray, (obj, father) => obj['SUB_ACCOUNT_NAME'] != null && obj['TERTIARY_ACCOUNT_NAME'] == null && obj["MASTER_ACCOUNT_NAME"] == father["MASTER_ACCOUNT_NAME"])

您可以将其称为第二级。然后为每个第一级节点一次,传递其子级,当然,更改用于选择子级的lambda /函数。

您应该在两个Lambda之间更改最少的内容。如果可以具有更多的级别,则可以将其进一步推广。


0
投票

您可以将reduce方法与while循环和数组结合使用,以将不同的值存储在将从另一个props对象映射的值上。

const data = [{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":null,"TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":6,"OVERALL_MARKET_VALUE":1},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":1,"OVERALL_MARKET_VALUE":8},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_1","MARKET_VALUE":10,"OVERALL_MARKET_VALUE":5},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_2","MARKET_VALUE":5,"OVERALL_MARKET_VALUE":3},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":5,"OVERALL_MARKET_VALUE":10},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_1","MARKET_VALUE":6,"OVERALL_MARKET_VALUE":1},{"MASTER_ACCOUNT_NAME":"MAIN_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_2","MARKET_VALUE":10,"OVERALL_MARKET_VALUE":5},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":null,"TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":6,"OVERALL_MARKET_VALUE":1},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":1,"OVERALL_MARKET_VALUE":8},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_1","MARKET_VALUE":10,"OVERALL_MARKET_VALUE":5},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"MAIN_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_2","MARKET_VALUE":5,"OVERALL_MARKET_VALUE":3},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":null,"MARKET_VALUE":5,"OVERALL_MARKET_VALUE":10},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_1","MARKET_VALUE":6,"OVERALL_MARKET_VALUE":1},{"MASTER_ACCOUNT_NAME":"SECOND_ENTITY","SUB_ACCOUNT_NAME":"SECOND_ACCOUNT","TERTIARY_ACCOUNT_NAME":"TERTIARY_ACCOUNT_2","MARKET_VALUE":10,"OVERALL_MARKET_VALUE":5}]

const levels = {
  "MASTER_ACCOUNT_NAME": 0,
  "SUB_ACCOUNT_NAME": 1,
  "TERTIARY_ACCOUNT_NAME": 2
}

const result = []

data.reduce((r, e) => {
  let value = { ...e, children: [] }
  let level = Object.keys(levels).length;

  while (level--) {
    if (e[Object.keys(levels)[level]]) {
      r[level + 1] = value.children
      r[level].push(value)
      break
    }
  }

  return r;
}, [result])

console.log(result)
© www.soinside.com 2019 - 2024. All rights reserved.