让我们说我有一个对象机场与成员airportCode和airportCity像这样:
function Airport(airportCode, airportCity) {
this.airportCode = airportCode;
this.airportCity = airportCity;
};
如何创建我可以添加的机场数组。在Java中,这可以这样工作:
while(st.hasMoreTokens()) {
Airport a = new Airport();
a.airportCode = st.nextToken();
a.airportCity = st.nextToken();
airports.add(a);
}
答案 0 :(得分:10)
答案非常简短:
airports.push(new Airport("code","city"));
答案 1 :(得分:6)
试试这个:
function Airport(airportCode, airportCity) {
this.airportCode = airportCode;
this.airportCity = airportCity;
};
var dataArray = [];
for(var i=0; i< 10; i++){
dataArray[i] = new Airport("Code-" + i, "City" + i);
}
答案 2 :(得分:4)
它并不是那么不同
var airports = [];
while (st.hasMoreTokens()) {
var a = new Airport();
a.airportCode = st.nextToken();
a.airportCity = st.nextToken();
airports.push(a);
}