2021-06-15 00:49:18 -07:00
---
id: 567af2437cbaa8c51670a16c
2021-07-21 20:53:20 +05:30
title: Testar objetos por propriedades
2021-06-15 00:49:18 -07:00
challengeType: 1
forumTopicId: 18324
dashedName: testing-objects-for-properties
---
# --description--
2021-07-30 01:41:44 +09:00
Às vezes, é útil verificar se a propriedade de um determinado objeto existe ou não. Podemos usar o método de objetos `.hasOwnProperty(propname)` para determinar se aquele objeto possui o nome de propriedade fornecido. `.hasOwnProperty()` retorna `true` ou `false` se a propriedade for encontrada ou não.
2021-06-15 00:49:18 -07:00
2021-07-14 21:02:51 +05:30
**Exemplo**
2021-06-15 00:49:18 -07:00
```js
2021-10-27 15:10:57 +00:00
const myObj = {
2021-06-15 00:49:18 -07:00
top: "hat",
bottom: "pants"
};
2021-10-27 15:10:57 +00:00
2021-06-15 00:49:18 -07:00
myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");
```
2021-07-14 21:02:51 +05:30
O primeiro `hasOwnProperty` retorna `true` , enquanto o segundo retorna `false` .
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-14 21:02:51 +05:30
Modifique a função `checkObj` para verificar se um objeto passado para a função (`obj` ) contém uma propriedade específica (`checkProp` ). Se a propriedade for encontrada, retorne o valor da propriedade. Se não, retorne `"Not Found"` .
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-14 21:02:51 +05:30
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` deve retornar a string `pony` .
2021-06-15 00:49:18 -07:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
);
```
2021-07-14 21:02:51 +05:30
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` deve retornar a string `kitten` .
2021-06-15 00:49:18 -07:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
);
```
2021-07-14 21:02:51 +05:30
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` deve retornar a string `Not Found` .
2021-06-15 00:49:18 -07:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
'Not Found'
);
```
2021-07-14 21:02:51 +05:30
`checkObj({city: "Seattle"}, "city")` deve retornar a string `Seattle` .
2021-06-15 00:49:18 -07:00
```js
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
```
2021-07-14 21:02:51 +05:30
`checkObj({city: "Seattle"}, "district")` deve retornar a string `Not Found` .
2021-06-15 00:49:18 -07:00
```js
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
```
2021-07-14 21:02:51 +05:30
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` deve retornar a string `Not Found` .
2021-06-15 00:49:18 -07:00
```js
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
```
# --seed--
## --seed-contents--
```js
function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
```
# --solutions--
```js
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
```