An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
April 15, 2021 10:26 pm GMT
Original Link: https://dev.to/frantchessico/javascript-array-find-method-d0h
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', last_name: 'Inoque', email: '[email protected]', username: '@franciscoinoque' }, { user_id: '5678', first_name: 'Jose', last_name: 'David', email: '[email protected]', username: '@josedavid' }, { user_id: '9101', first_name: 'Peter', last_name: 'Jordan', email: '[email protected]', username: '@peterjordan' }, { user_id: '1112', first_name: 'Clifton', last_name: 'Urik', email: '[email protected]', 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)
Original Link: https://dev.to/frantchessico/javascript-array-find-method-d0h
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To