1

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.