Home > Mobile >  How do rearrange the nested array
How do rearrange the nested array

Time:03-17

Below is my response which is getting from the MySQL database,

I want to rearrange childInterest like in the second desired output

"childInterest": [
        {
          "id": 1,
          "parentInterestId": 3,
          "created_at": "2022-03-04T07:30:31.851Z",
          "updated_at": "2022-03-04T07:30:31.851Z",
          "interest": {
            "id": 6,
            "interestId": 3,
            "name": "collecting stamps",
            "level": 2,
            "createdAt": "2022-01-27T14:22:22.586Z",
            "updatedAt": "2022-01-27T14:22:22.586Z",
            "heirarchy": {
              "id": 3,
              "interestId": null,
              "name": "hobbies",
              "level": 1,
              "createdAt": "2022-01-27T14:20:40.362Z",
              "updatedAt": "2022-01-27T14:20:40.362Z"
            }
          }
        },
        {
          "id": 2,
          "parentInterestId": 3,
          "created_at": "2022-03-04T07:30:31.863Z",
          "updated_at": "2022-03-04T07:30:31.863Z",
          "interest": {
            "id": 7,
            "interestId": 3,
            "name": "chess play",
            "level": 2,
            "createdAt": "2022-01-27T14:22:48.734Z",
            "updatedAt": "2022-01-27T14:22:48.734Z",
            "heirarchy": {
              "id": 3,
              "interestId": null,
              "name": "hobbies",
              "level": 1,
              "createdAt": "2022-01-27T14:20:40.362Z",
              "updatedAt": "2022-01-27T14:20:40.362Z"
            }
          }
        },
        {
          "id": 5,
          "parentInterestId": 4,
          "created_at": "2022-03-04T08:06:03.499Z",
          "updated_at": "2022-03-04T08:06:03.499Z",
          "interest": {
            "id": 8,
            "interestId": 4,
            "name": "cricket",
            "level": 2,
            "createdAt": "2022-01-27T14:23:13.042Z",
            "updatedAt": "2022-01-27T14:23:13.042Z",
            "heirarchy": {
              "id": 4,
              "interestId": null,
              "name": "outdoor",
              "level": 1,
              "createdAt": "2022-01-27T14:21:00.701Z",
              "updatedAt": "2022-01-27T14:21:00.701Z"
            }
          }
        },
        {
          "id": 6,
          "parentInterestId": 4,
          "created_at": "2022-03-04T08:06:03.519Z",
          "updated_at": "2022-03-04T08:06:03.519Z",
          "interest": {
            "id": 9,
            "interestId": 4,
            "name": "coco",
            "level": 2,
            "createdAt": "2022-01-27T14:23:22.940Z",
            "updatedAt": "2022-01-27T14:23:22.940Z",
            "heirarchy": {
              "id": 4,
              "interestId": null,
              "name": "outdoor",
              "level": 1,
              "createdAt": "2022-01-27T14:21:00.701Z",
              "updatedAt": "2022-01-27T14:21:00.701Z"
            }
          }
        }
      ],

And I want a response like below

  "childInterest": [
      {
        title: "Hobbies",
        titleitems: [
          "Music",
          "Video"
        ]
      },
      {
        title: "Indoor",
        titleItems: [
          "Carrom",
          "Table Tenis"
        ]
      },
      {
        title: "Outdoor",
        titleItems: [
          "Cricket",
          "Kabbadi"
        ]
      }
    ]
  }
  ],


In 1st array whatever names hobbies, outdoor is there should come under single object inside that array should hobbies names like music video etc.

CodePudding user response:

You probably want to take a look at the map() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

CodePudding user response:

My solution.

I won't be explaining steps by steps.

let childInterest = [
  {
    id: 1,
    parentInterestId: 3,
    created_at: "2022-03-04T07:30:31.851Z",
    updated_at: "2022-03-04T07:30:31.851Z",
    interest: {
      id: 6,
      interestId: 3,
      name: "collecting stamps",
      level: 2,
      createdAt: "2022-01-27T14:22:22.586Z",
      updatedAt: "2022-01-27T14:22:22.586Z",
      heirarchy: {
        id: 3,
        interestId: null,
        name: "hobbies",
        level: 1,
        createdAt: "2022-01-27T14:20:40.362Z",
        updatedAt: "2022-01-27T14:20:40.362Z",
      },
    },
  },
  {
    id: 2,
    parentInterestId: 3,
    created_at: "2022-03-04T07:30:31.863Z",
    updated_at: "2022-03-04T07:30:31.863Z",
    interest: {
      id: 7,
      interestId: 3,
      name: "chess play",
      level: 2,
      createdAt: "2022-01-27T14:22:48.734Z",
      updatedAt: "2022-01-27T14:22:48.734Z",
      heirarchy: {
        id: 3,
        interestId: null,
        name: "hobbies",
        level: 1,
        createdAt: "2022-01-27T14:20:40.362Z",
        updatedAt: "2022-01-27T14:20:40.362Z",
      },
    },
  },
  {
    id: 5,
    parentInterestId: 4,
    created_at: "2022-03-04T08:06:03.499Z",
    updated_at: "2022-03-04T08:06:03.499Z",
    interest: {
      id: 8,
      interestId: 4,
      name: "cricket",
      level: 2,
      createdAt: "2022-01-27T14:23:13.042Z",
      updatedAt: "2022-01-27T14:23:13.042Z",
      heirarchy: {
        id: 4,
        interestId: null,
        name: "outdoor",
        level: 1,
        createdAt: "2022-01-27T14:21:00.701Z",
        updatedAt: "2022-01-27T14:21:00.701Z",
      },
    },
  },
  {
    id: 6,
    parentInterestId: 4,
    created_at: "2022-03-04T08:06:03.519Z",
    updated_at: "2022-03-04T08:06:03.519Z",
    interest: {
      id: 9,
      interestId: 4,
      name: "coco",
      level: 2,
      createdAt: "2022-01-27T14:23:22.940Z",
      updatedAt: "2022-01-27T14:23:22.940Z",
      heirarchy: {
        id: 4,
        interestId: null,
        name: "outdoor",
        level: 1,
        createdAt: "2022-01-27T14:21:00.701Z",
        updatedAt: "2022-01-27T14:21:00.701Z",
      },
    },
  },
];

let newOne = Object.values(
  childInterest.reduce(
    (
      acc,
      {
        interest: {
          name: titleItems,
          heirarchy: { name: title },
        },
      }
    ) => {
      const key = [title];
      acc[key] ||= { titleItems: [], title };
      acc[key].titleItems.push(titleItems);
      return acc;
    },
    {}
  )
);

console.log(newOne);
  • Related