Home > Mobile >  Ideal Way of Mapping Nested Data in JS
Ideal Way of Mapping Nested Data in JS

Time:07-08

I have successfully mapped the nested array but I need to know if there is a better way to do this? I wanted to follow the latest standard of JS

EXPECTED OUTPUT

[
  {
    "id": 123,
    "createdTs": "2018-11-07T04:55:00.000 00:00",
    "modifiedTs": "2022-03-17T23:29:06.000 00:00",
    "email": "[email protected]",
    "name": "Dayanara",
    "active": true,
    "lastLogin": "2020-10-28T03:22:22.000 00:00",
    "supplier": "Moscow",
    "group": "AA1"
  },
  {
    "id": 456,
    "createdTs": "2018-10-28T22:42:57.000 00:00",
    "modifiedTs": "2020-06-01T05:01:11.000 00:00",
    "email": "[email protected]",
    "name": "John Jones",
    "active": true,
    "lastLogin": "2020-06-01T05:00:35.000 00:00",
    "supplier": null,
    "group": "AA1"
  },
  {
    "id": 789,
    "createdTs": "2022-01-28T05:21:37.000 00:00",
    "modifiedTs": "2022-02-04T06:24:54.000 00:00",
    "email": "[email protected]",
    "name": "Gasmund",
    "active": true,
    "lastLogin": null,
    "supplier": "Ukraine",
    "group": "AA1"
  },
  {
    "id": 10112,
    "createdTs": "2022-07-07T09:51:14.000 00:00",
    "modifiedTs": "2022-07-07T09:51:14.000 00:00",
    "email": "[email protected]",
    "name": "deqwd",
    "active": true,
    "lastLogin": null,
    "supplier": "",
    "group": "AA2"
  }
]

const oldData = [
  {
    "uid": "AA1",
    "members": [
      {
        "id": 123,
        "createdTs": "2018-11-07T04:55:00.000 00:00",
        "modifiedTs": "2022-03-17T23:29:06.000 00:00",
        "uid": "[email protected]",
        "name": "Dayanara",
        "active": true,
        "lastLogin": "2020-10-28T03:22:22.000 00:00",
        "supplier": "Moscow"
      },
      {
        "id": 456,
        "createdTs": "2018-10-28T22:42:57.000 00:00",
        "modifiedTs": "2020-06-01T05:01:11.000 00:00",
        "uid": "[email protected]",
        "name": "John Jones",
        "active": true,
        "lastLogin": "2020-06-01T05:00:35.000 00:00",
        "supplier": null
      },
      {
        "id": 789,
        "createdTs": "2022-01-28T05:21:37.000 00:00",
        "modifiedTs": "2022-02-04T06:24:54.000 00:00",
        "uid": "[email protected]",
        "name": "Gasmund",
        "active": true,
        "lastLogin": null,
        "supplier": "Ukraine"
      }
    ]
  },
  {
    "uid": "AA2",
    "members": [
      {
        "id": 10112,
        "createdTs": "2022-07-07T09:51:14.000 00:00",
        "modifiedTs": "2022-07-07T09:51:14.000 00:00",
        "uid": "[email protected]",
        "name": "deqwd",
        "active": true,
        "lastLogin": null,
        "supplier": ""
      }
    ]
  }
]

const newData =  oldData.flatMap((groupUsers) => {
      return groupUsers.members.map((member) => {
        return {
          id: member.id,
          name: member.name,
          email: member.uid,
          active: member.active,
          group: groupUsers.uid,
          createdTs: member.createdTs,
          lastLogin: member.lastLogin,
          supplier: member.supplier,
        }
      })
    })
    
    console.log(newData)

CodePudding user response:

You want to exclude a property (modifiedTs) and modify a property (uid). Destructure with rest syntax to select those two properties and put the rest into an object, then create a new object including the modified property and the property from the outer groupUsers.

const oldData=[{uid:"AA1",members:[{id:123,createdTs:"2018-11-07T04:55:00.000 00:00",modifiedTs:"2022-03-17T23:29:06.000 00:00",uid:"[email protected]",name:"Dayanara",active:!0,lastLogin:"2020-10-28T03:22:22.000 00:00",supplier:"Moscow"},{id:456,createdTs:"2018-10-28T22:42:57.000 00:00",modifiedTs:"2020-06-01T05:01:11.000 00:00",uid:"[email protected]",name:"John Jones",active:!0,lastLogin:"2020-06-01T05:00:35.000 00:00",supplier:null},{id:789,createdTs:"2022-01-28T05:21:37.000 00:00",modifiedTs:"2022-02-04T06:24:54.000 00:00",uid:"[email protected]",name:"Gasmund",active:!0,lastLogin:null,supplier:"Ukraine"}]},{uid:"AA2",members:[{id:10112,createdTs:"2022-07-07T09:51:14.000 00:00",modifiedTs:"2022-07-07T09:51:14.000 00:00",uid:"[email protected]",name:"deqwd",active:!0,lastLogin:null,supplier:""}]}];

const newData =  oldData.flatMap((groupUsers) => (
  groupUsers.members.map(({ modifiedTs, uid, ...rest }) => ({
    email: uid,
    group: groupUsers.uid,
    ...rest
  }))
));
console.log(newData)

  • Related