Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
Convert Nested Array into Array of Strings in JS

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": "d@yahoo.com",
        "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": "j@yahoo.com",
        "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": "g@gmail.com",
        "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": "aa@yahoo.com",
        "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": "aa33@yahoo.com",
        "name": "fewfewffwef",
        "active": true,
        "lastLogin": null,
        "supplier": "USA"
      }
    ]
  }
]

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

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

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: "d@yahoo.com", 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: "j@yahoo.com", 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: "g@gmail.com", 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: "aa@yahoo.com", 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);

about 4 years ago · Santiago Gelvez Denunciar

0

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

about 4 years ago · Santiago Gelvez Denunciar

0

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), [])
));                    
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda