Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,319 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,319 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