【问题标题】:Parse Nested Level Json In javascript在 javascript 中解析嵌套级别 Json
【发布时间】:2021-10-22 00:51:53
【问题描述】:

示例输入:

[
  {
    "id": "p1",
    "top": 130,
    "left": 298,
    "Key": "test1",
    "Next": "special"
  },
  {
    "id": "p2",
    "Key": "special",
    "specialkey": [
      {"key": "1", "value": "p3"},
      {"key": "0", "value": "p4"},
      {"key": "2", "value": "p5"}
    ],
    "Next": "",
    "RepeatText": "p8",
    "RepeatTextNew": "p9",
  },
  {
    "id": "p3",
    "user": "aa",
    "Key": "test3",
    "Text": "hi"
  },
  {
    "id": "p4",
    "Key": "special",
    "specialkey": [
      {"key": "1", "value": "p6"},
      {"key": "0", "value": "p7"}
    ]
  },
  {
    "id": "p5",
    "user": "aa",
    "Key": "test5",
    "Text": "hi"
  },
  {
    "id": "p6",
    "user": "aa",
    "Key": "test6",
    "Text": "hi"
  },
  {
    "id": "p7",
    "user": "aa",
    "Key": "test7",
    "Text": "hi"
  },
  {
    "id": "p8",
    "user": "aa",
    "Key": "test8",
    "Text": "hi"
  },
 {
    "id": "p9",
    "user": "aa",
    "Key": "test9",
    "Text": "hi"
  }
]

样本输出:

{
  "test1": {
    "id": "p1",
    "top": 130,
    "left": 298,
    "Key": "test1",
    "Next": {
      "special": {
        "id": "p2",
        "Key": "special",
        "Next": "",
        "RepeatText": {
          "p8": {
            "id": "p8",
            "user": "aa",
            "Key": "test8",
            "Text": "hi"
          }
        },
        "RepeatTextNew": {
          "p9": {
            "id": "p9",
            "user": "aa",
            "Key": "test9",
            "Text": "hi"
          }
        },
        "specialkey": [
          {
            "key": "1",
            "value": {
              "id": "p3",
              "user": "aa",
              "Key": "test3",
              "Text": "hi"
            }
          },
          {
            "key": "0",
            "value": {
              "id": "p4",
              "Key": "special",
              "specialkey": [
                {
                  "key": "1",
                  "value": {
                    "id": "p6",
                    "user": "aa",
                    "Key": "test6",
                    "Text": "hi"
                  }
                },
                {
                  "key": "0",
                  "value": {
                    "id": "p7",
                    "user": "aa",
                    "Key": "test7",
                    "Text": "hi"
                  }
                }
              ]
            }
          },
          {
            "key": "2",
            "value": {
              "id": "p5",
              "user": "aa",
              "Key": "test5",
              "Text": "hi"
            }
          }
        ]
      }
    }
  }
}

当key等于special时,它可以有一个嵌套结构,我们只需要匹配下一个key

使用下面的代码,我无法达到预期的输出。

const processObject = ({ Next, ...rest }) => {
  const result = { ...rest };
  if (formatData.find((y) => y.Key == 'special')) {
    
    const nextObject = formatData.find((y) => y.Key == 'special')
    if (nextObject.specialkey) {
      for (let i = 0; i < nextObject.specialkey.length; i++) {
        let currentObject = formatData.find((y) => y.id === nextObject.specialkey[i].value)
        nextObject.specialkey[i].value = currentObject
      }
            
      result.Next = {
        [nextObject.Key]: processObject(nextObject),
      };
    }
  }
  if (Next) {
    const nextObject = formatData.find((y) => y.id === Next);
    result.Next = {
      [nextObject.Key]: processObject(nextObject),
    };
  }
  return result;
};
    
const response = {
  [formatData[0].Key]: processObject(formatData[0]),
};
return response

【问题讨论】:

    标签: javascript node.js json typescript


    【解决方案1】:

    这就是你所追求的吗?

    const input = [
      {
        "id": "p1", "top": 130, "left" :298, "Key": "test1",
        // I've changed this from "special" to "p2"
        "Next": "p2"
      // rest of input is the same...
      },{"id":"p2","Key":"special","specialkey":[{"key":"1","value":"p3"},{"key":"0","value":"p4"},{"key":"2","value":"p5"}],"Next":"","RepeatText": "p8","RepeatTextNew":"p9"},{"id":"p3","user":"aa","Key":"test3","Text":"hi"},{"id":"p4","Key":"special","specialkey":[{"key":"1","value":"p6"},{"key":"0","value":"p7"}]},{"id":"p5","user":"aa","Key":"test5","Text":"hi"},{"id":"p6","user":"aa","Key":"test6","Text":"hi"},{"id":"p7","user":"aa","Key":"test7","Text":"hi"},{"id":"p8","user":"aa","Key":"test8","Text":"hi"},{"id":"p9","user":"aa","Key":"test9","Text": "hi"}];
    
    // Gets an object by its id
    const getById = id => input.find(x => x.id === id);
    
    const processObject = ({ Next, specialkey, RepeatText, RepeatTextNew, ...rest }) => {
      let processedNext;
      if (Next) {
        const nextObject = getById(Next);
        processedNext = { [nextObject.Key]: processObject(nextObject) };
      }
      return {
        ...rest,
        // This spread syntax means we don't add the Next or
        // specialkey property if it isn't present in the input
        // object
        ...processedNext ? { Next: processedNext } : {},
        ...RepeatText
          ? { RepeatText: { [RepeatText]: processObject(getById(RepeatText)) } }
          : {},
        ...RepeatTextNew
          ? { RepeatTextNew: { [RepeatTextNew]: processObject(getById(RepeatTextNew)) } }
          : {},
        ...specialkey
          ? {
            specialkey: specialkey.map(({ key, value }) => ({
              key,
              value: processObject(getById(value))
            }))
          }
          : {}
      };
    }
    
    console.log(processObject(input[0]));

    在您的代码中,您似乎是通过 id 查找对象,这就是为什么我将第一个对象输入的 Next"special"Keyp2 对象)更改为 @ 987654327@.

    【讨论】:

    • Json 也可以有一些额外的键,例如 RepeatText 和 RepeatTextNew。我已经更新了输入和预期输出。请检查
    • @user13000875 我已经更新了我的答案。下次,请在您的问题中包含所有信息。如果有人回答了您的问题,而您回来说“还有更多我一开始没有告诉您的内容”,那么回答者可能会感到沮丧,并且可能会阻止他们回答您的问题。
    猜你喜欢
    • 2016-10-17
    • 2015-04-23
    • 2021-05-06
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2018-04-22
    相关资源
    最近更新 更多