const users = [
{
name: 'Dima',
age: 39,
phone: '099-245-45-45'
},
{
name: 'Vasya',
age: 28,
phone: '066-500-49-45'
},
{
name: 'Kolya',
age: 39,
phone: '063-100-20-13'
}
];
let userKey = 'Dima';
getUserData(userKey, users);
function getUserData(userKey, userArray) {
for (let i = 0; i < userArray.length; i++) {
if ((userArray[i].name == userKey) || (userArray[i].phone == userKey)) {
console.log('Имя пользователя: ' + userArray[i].name);
console.log('Возраст пользователя: ' + userArray[i].age);
console.log('Телефон пользователя: ' + userArray[i].phone);
return userArray[i]
}
}
console.log('Пользователь в БД не найден');
}
или тоже самое:
const users = [
{
name: 'Dima',
age: 39,
phone: '099-245-45-45'
},
{
name: 'Vasya',
age: 28,
phone: '066-500-49-45'
},
{
name: 'Kolya',
age: 39,
phone: '063-100-20-13'
}
];
const getUserData = key => users.find(u => u.name.toLocaleLowerCase() === key.toLocaleLowerCase() || u.phone === key) // dima === DiMa
// null = not found
console.log(getUserData('dima'))