3

Say I want the object to be something like this:

var Book = {
        title: "the catcher in the rye",
        price: "80.98",
        characters: [{name: "holden caulfield", age: 16, height:"6.2"},
                     {name: "phoebe caulfield",age:13, height: "5"}] 
     };

EDITED

question: characters array is built by adding a character one by one. How can do this while making sure that name, age and height properties are defined as above.

Something to the effect of?

Book.characters.add({name: "phoebe caulfield",age:13, height: "5"});

I would like to be able to define this programmatically, ie add properties to the object rather than define it like this.

Is this possible?

5
  • 1
    I don't get it, aren't you defining properties programmatically already? Commented Nov 16, 2011 at 0:05
  • 1
    Being very picky: that's an object literal, not a JSON object. JSON requires that keys be in quotes too ("title"). Commented Nov 16, 2011 at 0:06
  • @Corbin: thanks for pointing it out. Yes, this is an object, not JSON. Commented Nov 16, 2011 at 0:10
  • developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… Commented Nov 16, 2011 at 0:11
  • I believe the guys already answered your question (the edited version): book.characters.push("{'':'','':'','':'',....}") Commented Nov 16, 2011 at 0:25

6 Answers 6

7

You can do it in dynamic code (rather than a static declaration) like this:

var Book = {};

Book.title = "the catcher in the rye";
Book.price = "80.98";
Book.characters = [];
Book.characters.push({name: "holden caulfield", age: 16, height: "6.2"});
Book.characters.push({name: "phoebe caulfield", age: 13, height: "5"});
Sign up to request clarification or add additional context in comments.

Comments

2

Do you mean like this?

var Book = {}; // empty object

Book.title = "the catcher in the rye";
Book.price = 80.98;
Book.characters = [];

var character = {
    "name": "holden caulfield",
    "age": 16,
    "height": 6.2
};

Book.characters.push(character);

Comments

0
var Book={};
Book.title="the catcher in the rye";
Book.price="80.98";
var characters=[];
characters.push({name: "holden caulfield", age: 16, height:"6.2"});
characters.push({name: "phoebe caulfield",age:13, height: "5"});
Book.characters=characters;

...etc.

Comments

0

It certainly is! Javascript is a completely dynamic language - if you want a new property on an object, just set it!

e.g.

var myObject = {}; // empty object
myObject.myProperty = 5; // creates the new property and sets it to 5.
myObject.nestedObject = { prop1: 6, 'long-property-name': 'lolcats' };

Comments

0

I think you're looking for a JSON stringifier: http://www.json.org/js.html

This will allow you to create your object:

var myobj = {  };
myobj.myprop = "value";
alert(JSON.stringify(myobj));

1 Comment

sorry for confusing...the question is about objects not json. I will revise the header of the question.
0

using the map function is the easiest way I've found to build an array of objects. The snippet below, constructs the objet you want in just 2 statements:

var Book  = {
  title: "the catcher in the rye",
  price: 80.98
};

Book.characters = [6.2, 5].map(el => {
  return {
    name: 'caulfield', age: 14, height: el
  };
});



// Output
JSON.stringify(Book);
{
   "title": "the catcher in the rye",
   "price": 80.98,
   "characters": [
     { "name":"caulfield", "age":14, "height":6.2 }, 
     { "name":"caulfield", "age":14, "height":5 }
   ]
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.