0

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

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

2 Comments

Json can also have few extra key like RepeatText and RepeatTextNew. I have updated the input and expected output.Please check
@user13000875 I’ve updated my answer. Next time, please include all information in your question. If someone answers your question and you come back and say ‘there's more I didn’t tell you in the first place’, that can be frustrating for the answerer and can discourage them from answering your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.