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

122
Vistas
How to get list with of property from array of objects unless it contains another item with certain value?

I have an array of objects, and I need to get list with certain property from that array of objects. But i need that list to contain only those values where object was containing another property with certain element. This is very confusing so i made an example. Let's say i have an array with objects.

  employees = [
           {
            n: 'case 1',
            date: '2021-05-4',
            id: '123',
            user: [{name: 'Vlad', id: '1'}, {name: 'Misha', id: '2'}],
            isPresent : true,
           },
           {
            caseName: 'case 2',
            date: '2021-05-4',
            id: '124',
            user: [{name: 'Alina', id: '3'}, {name: 'Alex', id: '4'}],
            isPresent : true,
           },
           {
            caseName: 'case 3',
            date: '2021-05-4',
            id: '126',
            user: [],
            isPresent : false,
           },
        ]

And my task is to get a list of IDs from array of objects, but i need ID only from those objecrs which have isPresent as true. So i need ['123', '124'].

I could use a loops and conditions and so on. But i wondering is it possible to do with one line ? Something like this:

employees.filter(item => { return item.isPresent === true }))

But i need only IDs not whole objects.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

1) You can filter the elements with condition item.isPresent === true and then map over it to get the final result as:

employees
  .filter((item) => item.isPresent === true)
  .map((o) => o.id);

or you can also do as:

employees.filter((item) => item.isPresent).map((o) => o.id)

const employees = [{
    n: 'case 1',
    date: '2021-05-4',
    id: '123',
    user: [{
      name: 'Vlad',
      id: '1'
    }, {
      name: 'Misha',
      id: '2'
    }],
    isPresent: true,
  },
  {
    caseName: 'case 2',
    date: '2021-05-4',
    id: '124',
    user: [{
      name: 'Alina',
      id: '3'
    }, {
      name: 'Alex',
      id: '4'
    }],
    isPresent: true,
  },
  {
    caseName: 'case 3',
    date: '2021-05-4',
    id: '126',
    user: [],
    isPresent: false,
  },
]

const result = employees
  .filter((item) => item.isPresent === true)
  .map((o) => o.id);
console.log(result);

2) You can also achieve the same result using reduce as:

employees.reduce((acc, curr) => {
  curr.isPresent && acc.push(curr.id);
  return acc;
}, []);

const employees = [
  {
    n: "case 1",
    date: "2021-05-4",
    id: "123",
    user: [
      { name: "Vlad", id: "1" },
      { name: "Misha", id: "2" },
    ],
    isPresent: true,
  },
  {
    caseName: "case 2",
    date: "2021-05-4",
    id: "124",
    user: [
      { name: "Alina", id: "3" },
      { name: "Alex", id: "4" },
    ],
    isPresent: true,
  },
  {
    caseName: "case 3",
    date: "2021-05-4",
    id: "126",
    user: [],
    isPresent: false,
  },
];

const result = employees.reduce((acc, curr) => {
  curr.isPresent && acc.push(curr.id);
  return acc;
}, []);
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use something like this

employees.filter((item) => item.isPresent).map((obj) => obj.id);
about 4 years ago · Juan Pablo Isaza 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