Modify an Array Stored in an Object (Object.keys doesn't work)

Tell us what’s happening:

I used “return Object.keys(userObj.data.friends);” but it doesn’t work. can anybody explain to me why this isn’t working?

Thank you :slight_smile:

Your code so far


let user = {
  name: 'Kenneth',
  age: 28,
  data: {
    username: 'kennethCodesAllDay',
    joinDate: 'March 26, 2016',
    organization: 'freeCodeCamp',
    friends: [
      'Sam',
      'Kira',
      'Tomo'
    ],
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'
    }
  }
};

function addFriend(userObj, friend) {
  // change code below this line  

  user.data.friends.push(friend);
  return Object.keys(userObj.data.friends);

  // change code above this line
}

console.log(addFriend(user, 'Pete'));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/modify-an-array-stored-in-an-object

My guess is because you’re asking for the keys of an array.

return Object.keys(userObj.data.friends);

userObj.data.friends === ['Sam', 'Kira', 'Tomo'];

Object.keys(userObj.data.friends) === [ '0', '1', '2' ]

You are right.

Object.keys() method returns the array of Key values of an Object.
to get the values

return userObj.data.friends;

is enough.