2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Add Key-Value Pairs to JavaScript Objects
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Add Key-Value Pairs to JavaScript Objects
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
2018-10-12 15:37:13 -04:00
|
|
|
- The foods object has already been declared. All that is left to be done is to add three new `key-values`.
|
|
|
|
|
|
|
|
```javascript
|
2019-07-24 00:59:27 -07:00
|
|
|
OBJECT[{ KEY }] = { VALUE };
|
2018-10-12 15:37:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
- The above code will create a ney `key-value` within the object.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
let foods = {
|
|
|
|
apples: 25,
|
|
|
|
oranges: 32,
|
|
|
|
plums: 28
|
|
|
|
};
|
|
|
|
// change code below this line
|
2019-07-24 00:59:27 -07:00
|
|
|
foods["bananas"] = 13;
|
|
|
|
foods["grapes"] = 35;
|
|
|
|
foods["strawberries"] = 27;
|
2018-10-12 15:37:13 -04:00
|
|
|
// change code above this line
|
|
|
|
console.log(foods);
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|