Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Help JSON

Hi,

I'm trying to figure out how to add new properties to a JSON object? I've tried pretty much everything but nothing seems to work :-/

I.e. if you have properties and values like this:

var example = {
  1: {
    Name: "Henrik",
    Age: 28
  }
  2: {
    Name: "Henrik",
    Age: 28
  }
};

How to I add a new property and value to 1: or 2: like this?

Country: "Denmark"

1 Answer

Viren Mahtani
Viren Mahtani
924 Points
var example = {
  1: {
    Name: "Henrik",
    Age: 28
  },
  2: {
    Name: "Henrik",
    Age: 28
  }
};

example["1"].country = 'Denmark';
example["2"].country = 'Philippines';

You're missing a comma ( , ) after the first property of your object Cheers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Thanks a million times! You just saved my day! :-D

Yes now I see the missing comma, but I just made the example very quick because I just couldn't figure out how to add new properties and values :-P