Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading