Add Key-Value Pairs to JavaScript Objects

Tell us what’s happening:

How to do it ?

Your code so far


let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};

// change code below this line

// change code above this line

console.log(foods);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects

Add three more entries: bananas with a value of 13, grapes with a value of 35, and strawberries with a value of 27

Add new foods to the foods object.

/* 
    foods[<<key>>] = <<value>>
*/

foods['blueberries'] = 99;
1 Like

Hi, you can do this by using dot notation. So the answer is:

foods.bananas = 13;
foods.grapes = 35;
foods.strawberries = 27;

let foods = {
apples: 25,
oranges: 32,
plums: 28,

};

// change code below this line
foods[‘bananas’] = 13;
foods[‘grapes’] = 35;
foods[‘strawberries’] = 27;

// change code above this line

console.log(foods);

Do you have an issue with that code?

not working for me :confused:

use the ask for help button to create your own thread, so that your code will be included and people can help you based on what you have written

nvm it worked. i was just doing the work twice without realizing

This is helpful for all guys here.
Add new key-value pair to Javascript Object