js数组处理

将如下数组处理成目标多维数组
1.根据如下数组中的 id 和 targetReplayId 做匹配。
2.把如下数组中的有 targetReplayId 的项,放到与之对应的 id项下的children里面。
3.步骤2涉及到多层,需要使用递归。

const data = [
    {
      targetReplayId: "",
      targetReplayContent: "",
      currentReplayContent: "dfsghgd",
      id: "5f3ba9bdfabca1dc7a53e603",
    },
    {
      targetReplayId: "",
      targetReplayContent: "",
      currentReplayContent: "bbb",
      id: "5f3c962d6e3966e3af217f6f",
    },
    {
      targetReplayId: "5f3c962d6e3966e3af217f6f",
      targetReplayContent: "bbb",
      currentReplayContent: "擦擦擦",
      id: "5f3cc0160ca406e64941813b",
    },
    {
      targetReplayId: "5f3c962d6e3966e3af217f6f",
      targetReplayContent: "bbb",
      currentReplayContent: "去去去",
      id: "5f3cc2080ca406e649418148",
    },
    {
      targetReplayId: "5f3c962d6e3966e3af217f6f",
      targetReplayContent: "bbb",
      currentReplayContent: "回复XXX",
      id: "5f3cc9630ca406e6494181c3",
    },
    {
      targetReplayId: "5f3ba9bdfabca1dc7a53e603",
      targetReplayContent: "dfsghgd",
      currentReplayContent: "玩的可还高兴啊",
      id: "5f3cc9e00ca406e6494181d4",
    },
    {
      targetReplayId: "5f3c962d6e3966e3af217f6f",
      targetReplayContent: "bbb",
      currentReplayContent: "写的不错",
      id: "5f3cc9f30ca406e6494181d9",
    },
    {
      targetReplayId: "5f3c962d6e3966e3af217f6f",
      targetReplayContent: "bbb",
      currentReplayContent: "gogogo",
      id: "5f3ccb640ca406e6494181e2",
    },
    {
      targetReplayId: "5f3cc2080ca406e649418148",
      targetReplayContent: "去去去",
      currentReplayContent: "go1",
      id: "5f3ccb8a0ca406e6494181eb",
    }
];

目标数组

const targetData = [
    {
      targetReplayId: "",
      targetReplayContent: "",
      currentReplayContent: "dfsghgd",
      id: "5f3ba9bdfabca1dc7a53e603",
      children: [
        {
          targetReplayId: "5f3ba9bdfabca1dc7a53e603",
          targetReplayContent: "dfsghgd",
          currentReplayContent: "玩的可还高兴啊",
          id: "5f3cc9e00ca406e6494181d4"
        }
      ]
    },
    {
      targetReplayId: "",
      targetReplayContent: "",
      currentReplayContent: "bbb",
      id: "5f3c962d6e3966e3af217f6f",
      children: [
        {
          targetReplayId: "5f3c962d6e3966e3af217f6f",
          targetReplayContent: "bbb",
          currentReplayContent: "擦擦擦",
          id: "5f3cc0160ca406e64941813b"
        },
        {
          targetReplayId: "5f3c962d6e3966e3af217f6f",
          targetReplayContent: "bbb",
          currentReplayContent: "去去去",
          id: "5f3cc2080ca406e649418148",
          children: [
            {
              targetReplayId: "5f3cc2080ca406e649418148",
              targetReplayContent: "去去去",
              currentReplayContent: "go1",
              id: "5f3ccb8a0ca406e6494181eb"
            }
          ]
        },
        {
          targetReplayId: "5f3c962d6e3966e3af217f6f",
          targetReplayContent: "bbb",
          currentReplayContent: "回复XXX",
          id: "5f3cc9630ca406e6494181c3"
        },
        {
          targetReplayId: "5f3c962d6e3966e3af217f6f",
          targetReplayContent: "bbb",
          currentReplayContent: "写的不错",
          id: "5f3cc9f30ca406e6494181d9"
        },
        {
          targetReplayId: "5f3c962d6e3966e3af217f6f",
          targetReplayContent: "bbb",
          currentReplayContent: "gogogo",
          id: "5f3ccb640ca406e6494181e2"
        }
      ]
    }
  ];
阅读 1.7k
2 个回答

看起来像评论树的样子。平级列表转树,写了不下100次了。
可以看看我的文章,应该很容易理解,文中有列表转树的代码,大概6、7行的样子,改一下属性名就能完成你的需求:
https://wintc.top/article/20

试一下

function buildTree(list){
    let temp = {};
    let tree = {};
    for(let i in list){
        temp[list[i].id] = list[i];
    }
    for(let i in temp){
        if(temp[i].targetReplayId) {
            if(!temp[temp[i].targetReplayId].children) {
                temp[temp[i].targetReplayId].children = new Object();
            }
            temp[temp[i].targetReplayId].children[temp[i].id] = temp[i];
        } else {
            tree[temp[i].id] =  temp[i];
        }
    }
    return tree;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题