2018-09-30 23:01:58 +01:00
---
id: 587d7b7d367417b2b2512b1c
title: Check if an Object has a Property
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301155
2021-01-13 03:31:00 +01:00
dashedName: check-if-an-object-has-a-property
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the `hasOwnProperty()` method and the other uses the `in` keyword. If we have an object `users` with a property of `Alan` , we could check for its presence in either of the following ways:
2019-04-26 17:21:49 -07:00
2019-05-01 09:33:02 -07:00
```js
2019-04-26 17:21:49 -07:00
users.hasOwnProperty('Alan');
'Alan' in users;
```
2021-03-02 16:12:12 -08:00
Both of these would return `true` .
2020-11-27 19:02:05 +01:00
# --instructions--
2022-03-19 00:56:57 -07:00
Finish writing the function so that it returns `true` if the object passed to it contains all four names, `Alan` , `Jeff` , `Sarah` and `Ryan` and returns `false` otherwise.
2020-11-27 19:02:05 +01:00
# --hints--
2021-05-18 18:36:53 +02:00
The `users` object should not be accessed directly
```js
assert(code.match(/users/gm).length < = 2)
```
2020-11-27 19:02:05 +01:00
The `users` object should only contain the keys `Alan` , `Jeff` , `Sarah` , and `Ryan`
```js
assert(
'Alan' in users & &
'Jeff' in users & &
'Sarah' in users & &
'Ryan' in users & &
Object.keys(users).length === 4
);
```
2021-05-18 18:36:53 +02:00
The function `isEveryoneHere` should return `true` if `Alan` , `Jeff` , `Sarah` , and `Ryan` are properties on the object passed to it.
2020-11-27 19:02:05 +01:00
```js
assert(isEveryoneHere(users) === true);
```
2021-05-18 18:36:53 +02:00
The function `isEveryoneHere` should return `false` if `Alan` is not a property on the object passed to it.
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
delete users.Alan;
return isEveryoneHere(users);
})() === false
);
```
2021-05-18 18:36:53 +02:00
The function `isEveryoneHere` should return `false` if `Jeff` is not a property on the object passed to it.
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
delete users.Jeff;
return isEveryoneHere(users);
})() === false
);
2018-09-30 23:01:58 +01:00
```
2021-05-18 18:36:53 +02:00
The function `isEveryoneHere` should return `false` if `Sarah` is not a property on the object passed to it.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
delete users.Sarah;
return isEveryoneHere(users);
})() === false
);
```
2018-09-30 23:01:58 +01:00
2021-05-18 18:36:53 +02:00
The function `isEveryoneHere` should return `false` if `Ryan` is not a property on the object passed to it.
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
delete users.Ryan;
return isEveryoneHere(users);
})() === false
);
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
2021-05-18 18:36:53 +02:00
function isEveryoneHere(userObj) {
2020-03-02 23:18:30 -08:00
// Only change code below this line
2021-05-18 18:36:53 +02:00
2020-03-02 23:18:30 -08:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
console.log(isEveryoneHere(users));
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
2021-05-18 18:36:53 +02:00
function isEveryoneHere(userObj) {
2018-10-16 00:04:24 +01:00
return [
'Alan',
'Jeff',
'Sarah',
'Ryan'
2021-05-18 18:36:53 +02:00
].every(user => userObj.hasOwnProperty(user));
2018-10-15 16:29:05 -05:00
}
2018-10-20 21:02:47 +03:00
console.log(isEveryoneHere(users));
2018-09-30 23:01:58 +01:00
```