Vlad
Vlad

Reputation: 511

How to check if each objects in array contains certain value

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 it 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'}],
            isPresent : true,
           },
           {
            caseName: 'case 2',
            date: '2021-05-4',
            id: '123',
            user: [{name: 'Alina', id: '3'}, {name: 'Alex', id: '4'}],
            isPresent : true,
           },
           {
            caseName: 'case 3',
            date: '2021-05-4',
            id: '123',
            user: [],
            isPresent : true,
           },
        ]

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

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

Upvotes: 1

Views: 58

Answers (1)

Dario
Dario

Reputation: 6280

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)

Upvotes: 1

Related Questions