简体   繁体   中英

Parse Nested Level Json In javascript

Sample Input:

[
  {
    "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"
  }
]

Sample Output:

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

When the key is equal to special it can have a nested structure and for either we just need to match with the next key

With the below code, I am not able to achieve the expected output.

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

Is this what you're after?

 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]));

In your code, you seem to be looking up objects by their id , so that's why I changed the first object input's Next from "special" (the Key of the p2 object) to "p2" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM