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?
-
1I don't get it, aren't you defining properties programmatically already?NullUserException– NullUserException2011年11月16日 00:05:25 +00:00Commented Nov 16, 2011 at 0:05
-
1Being very picky: that's an object literal, not a JSON object. JSON requires that keys be in quotes too ("title").Corbin– Corbin2011年11月16日 00:06:53 +00:00Commented Nov 16, 2011 at 0:06
-
@Corbin: thanks for pointing it out. Yes, this is an object, not JSON.sarsnake– sarsnake2011年11月16日 00:10:02 +00:00Commented Nov 16, 2011 at 0:10
-
developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…Phil– Phil2011年11月16日 00:11:09 +00:00Commented Nov 16, 2011 at 0:11
-
I believe the guys already answered your question (the edited version): book.characters.push("{'':'','':'','':'',....}")zequinha-bsb– zequinha-bsb2011年11月16日 00:25:11 +00:00Commented Nov 16, 2011 at 0:25
6 Answers 6
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"});
Comments
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
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
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
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
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 }
]
}