Home > other >  Convert Nested Array into Array of Strings in JS
Convert Nested Array into Array of Strings in JS

Time:07-21

I need to extract the supplier property from nested arrays. Is there a better and simpler way to do this? Only display it once if it has duplicates

Expected Output should be like:

['Moscow', 'USA']

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": ""
      }
    ]
  },
  {
    "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": "USA"
      },
      {
        "id": 101123,
        "createdTs": "2022-07-07T09:51:14.000 00:00",
        "modifiedTs": "2022-07-07T09:51:14.000 00:00",
        "uid": "[email protected]",
        "name": "fewfewffwef",
        "active": true,
        "lastLogin": null,
        "supplier": "USA"
      }
    ]
  }
]

const newData =  oldData.flatMap((groupUsers) => (
  groupUsers.members.map(({ supplier }) => ({
   supplier: supplier
  }))
));
    
    console.log(newData)

CodePudding user response:

You could use filter to exclude the empty strings, flatMap to collect the rest of the values, and Set to get unique values:

const oldData = [{"members": [{"supplier": "Moscow"},{"supplier": null},{"supplier": ""}]},{"members": [{"supplier": "USA"},{"supplier": "USA"}]}];

const newData = Array.from(new Set(
    oldData.flatMap(({members}) =>
        members.map(({supplier}) => supplier).filter(Boolean)
    )
));

console.log(newData);

CodePudding user response:

A double Array.flatMap will help.

If you have duplicates and expect unique list as output, use can use Set

First flatMap to generate a list of members, second flatMap to generate the list of supplier and a Set to generate the unique list from this.

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: "" },
    ],
  },
  {
    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: "USA" },
    ],
  },
];

const newData = oldData
  .flatMap(({ members }) => members)
  .flatMap(({ supplier }) => (supplier ? supplier : []));

const uniqueSet = Array.from(new Set(newData));
console.log(newData);
console.log(uniqueSet);

CodePudding user response:

Alternative approach with Array.prototype.reduce:

const newData = Array.from(new Set(oldData
  .reduce((p, c) => p.concat(c.members.filter(m => !!m.supplier)) , [])
  .reduce((p, c) => p.concat(c.supplier), [])
));                    

CodePudding user response:

You could map just the supplier value and filter with a Set for truthy values.

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: "" }] }, { 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: "USA" }, { id: 101123, createdTs: "2022-07-07T09:51:14.000 00:00", modifiedTs: "2022-07-07T09:51:14.000 00:00", uid: "[email protected]", name: "fewfewffwef", active: true, lastLogin: null, supplier: "USA" }] }],
    newData = oldData
        .flatMap((groupUsers) => groupUsers.members.map(({ supplier }) => supplier))
        .filter((s => v => v && !s.has(v) && s.add(v))(new Set));
    
console.log(newData);

  • Related