2018-10-12 15:37:13 -04:00
---
title: Modify an Array Stored in an Object
---
2019-07-24 00:59:27 -07:00
# Modify an Array Stored in an Object
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
- The function can be written in just two lines of code.
- The first line should just use the `push()` function to add the `friend` parameter to the array found in `user.data.friend` . The second line will return the modified array.
- Remember that `user` mustb be referenced with the first parameter given to the `addFriend()` function.
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
```javascript
2018-10-12 15:37:13 -04:00
let user = {
2019-07-24 00:59:27 -07:00
name: "Kenneth",
2018-10-12 15:37:13 -04:00
age: 28,
data: {
2019-07-24 00:59:27 -07:00
username: "kennethCodesAllDay",
joinDate: "March 26, 2016",
organization: "freeCodeCamp",
friends: ["Sam", "Kira", "Tomo"],
2018-10-12 15:37:13 -04:00
location: {
2019-07-24 00:59:27 -07:00
city: "San Francisco",
state: "CA",
country: "USA"
2018-10-12 15:37:13 -04:00
}
}
};
function addFriend(userObj, friend) {
2019-07-24 00:59:27 -07:00
// change code below this line
2018-10-12 15:37:13 -04:00
userObj.data.friends.push(friend);
return userObj.data.friends;
// change code above this line
}
2019-07-24 00:59:27 -07:00
console.log(addFriend(user, "Pete"));
2018-10-12 15:37:13 -04:00
```
2019-07-24 00:59:27 -07:00
< / details >