I know how to create simple object and add dynamically properties to it. object = new Object(); object.someproperty = "";
我知道如何创建简单对象并为其添加动态属性。 object = new Object(); object.someproperty =“”;
However, I'm having hardtime creating dynamically something like this: (in javascript)
但是,我很难动态创建这样的东西:(在javascript中)
var datasets = {
"usa": {
label: "USA",
data: [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]]
},
"uk": {
label: "UK",
data: [[1988, 62982], [1989, 62027], [1990, 60696], [1991, 62348], [1992, 58560], [1993, 56393], [1994, 54579], [1995, 50818], [1996, 50554], [1997, 48276], [1998, 47691], [1999, 47529], [2000, 47778], [2001, 48760], [2002, 50949], [2003, 57452], [2004, 60234], [2005, 60076], [2006, 59213]]
},
.......................
.............(more...)
};
2 个解决方案
#1
dataset[country] = {label: countryName, data: theDataObject};
with
var country = 'usa';
var countryName = 'USA';
var theDataObject = [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]];
This is the JSON format.
这是JSON格式。
dataset.usa.label = 'USA';
or dataset.usa.label = countryName; // etc
或dataset.usa.label = countryName; //等
And :
dataset['usa']
is equal to
等于
dataset.usa
which is equal to
等于
dataset[country]
when the country variable is 'usa'.
当国家变量是'美国'时。
#2
Using the JSON notation as you demonstrate should work. It's just a matter of figuring out your syntax.
在演示时使用JSON表示法应该可行。这只是一个弄清楚语法的问题。
The notation allows you to create pretty complex structures in one statement:
该表示法允许您在一个语句中创建非常复杂的结构:
var continent = {
name: "North America",
countries: [
{ name: "USA",
states: ['AL', 'AK', 'AZ', ... ]
},
{ name: "Canada",
states: ['Ontario', 'Quebec', ... ]
}
]
}
And so on.
等等。
By the way, this also allows you to use the following shorthand for creating blank Objects:
顺便说一下,这也允许您使用以下速记创建空白对象:
var myObj = {};
#1
dataset[country] = {label: countryName, data: theDataObject};
with
var country = 'usa';
var countryName = 'USA';
var theDataObject = [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]];
This is the JSON format.
这是JSON格式。
dataset.usa.label = 'USA';
or dataset.usa.label = countryName; // etc
或dataset.usa.label = countryName; //等
And :
dataset['usa']
is equal to
等于
dataset.usa
which is equal to
等于
dataset[country]
when the country variable is 'usa'.
当国家变量是'美国'时。
#2
Using the JSON notation as you demonstrate should work. It's just a matter of figuring out your syntax.
在演示时使用JSON表示法应该可行。这只是一个弄清楚语法的问题。
The notation allows you to create pretty complex structures in one statement:
该表示法允许您在一个语句中创建非常复杂的结构:
var continent = {
name: "North America",
countries: [
{ name: "USA",
states: ['AL', 'AK', 'AZ', ... ]
},
{ name: "Canada",
states: ['Ontario', 'Quebec', ... ]
}
]
}
And so on.
等等。
By the way, this also allows you to use the following shorthand for creating blank Objects:
顺便说一下,这也允许您使用以下速记创建空白对象:
var myObj = {};