Home > Back-end >  How to check if each objects in array contains certain value
How to check if each objects in array contains certain value

Time:12-16

I need to check if each object in array contains a certain same value. I could do it with for loop, but i was wondering is it possible to do with one line of code. Let's say i have objects

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

And my task is to check if all employees are present, so i need to get true if isPesent property assigned true for each object. But have to do it with one live something like

let employeesPresent = employees(item => item.isPresent === true); ​

CodePudding user response:

You can just use every method of array, which tests if all item in the array pass the test implemented by the provided function:

let employeesPresent = employees.every(item => item.isPresent === true); ​

(Note that your are using isPesent prop in your array declaration but isPresent in your function)

  • Related