JavaScript Array find() Method

The find () method returns the value of the first element of the array that satisfies the test function provided. Otherwise, the undefined value is returned.

const users = [
{
user_id: ‘1234’,
first_name: ‘Francisco’,
las…


This content originally appeared on DEV Community and was authored by Francisco Inoque

The find () method returns the value of the first element of the array that satisfies the test function provided. Otherwise, the undefined value is returned.

const users = [
    {
        user_id: '1234',
        first_name: 'Francisco',
        last_name: 'Inoque',
        email: 'jaimeinoque20@gmail.com',
        username: '@franciscoinoque'
    },

    {
        user_id: '5678',
        first_name: 'Jose',
        last_name: 'David',
        email: 'josedavid@gmail.com',
        username: '@josedavid'
    },

    {
        user_id: '9101',
        first_name: 'Peter',
        last_name: 'Jordan',
        email: 'peterjordan@gmail.com',
        username: '@peterjordan'
    },

    {
        user_id: '1112',
        first_name: 'Clifton',
        last_name: 'Urik',
        email: 'cliftonurik@gmail.com',
        username: '@cliftonurik'
    }
]

let error_msg = {
     error: 'User not found'
 }
function findUserByUserID(user_id)
{
    const user = users.find(user => user.user_id === user_id);

    if (user)
    {
        return user;
    }  else
     {
        return error_msg
    }
}

const getUser = findUserByUserID('1112')
console.log(getUser)


This content originally appeared on DEV Community and was authored by Francisco Inoque


Print Share Comment Cite Upload Translate Updates
APA

Francisco Inoque | Sciencx (2021-04-15T22:26:17+00:00) JavaScript Array find() Method. Retrieved from https://www.scien.cx/2021/04/15/javascript-array-find-method/

MLA
" » JavaScript Array find() Method." Francisco Inoque | Sciencx - Thursday April 15, 2021, https://www.scien.cx/2021/04/15/javascript-array-find-method/
HARVARD
Francisco Inoque | Sciencx Thursday April 15, 2021 » JavaScript Array find() Method., viewed ,<https://www.scien.cx/2021/04/15/javascript-array-find-method/>
VANCOUVER
Francisco Inoque | Sciencx - » JavaScript Array find() Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/15/javascript-array-find-method/
CHICAGO
" » JavaScript Array find() Method." Francisco Inoque | Sciencx - Accessed . https://www.scien.cx/2021/04/15/javascript-array-find-method/
IEEE
" » JavaScript Array find() Method." Francisco Inoque | Sciencx [Online]. Available: https://www.scien.cx/2021/04/15/javascript-array-find-method/. [Accessed: ]
rf:citation
» JavaScript Array find() Method | Francisco Inoque | Sciencx | https://www.scien.cx/2021/04/15/javascript-array-find-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.