Home > OS >  How to get list with of property from array of objects unless it contains another item with certain
How to get list with of property from array of objects unless it contains another item with certain

Time:12-20

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.

CodePudding user response:

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

CodePudding user response:

You can use something like this

employees.filter((item) => item.isPresent).map((obj) => obj.id);
  • Related