Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

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"
        }
      ]
    }
  ];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...