我有这个示例数组,用于将条目插入到 YUI 数据表中
var book = {
"id" : "po-0167",
"date" : new Date(1980, 2, 24),
"quantity" : 1,
"amount" : 4,
"title" : "A Book About Nothing"
};
我可以通过这样做获得相同的数组吗?
var book = [];
var booktemp = {
"id" : "po-0167"
};
book.push(booktemp);
booktemp = {
"date" : new Date(1980, 2, 24)
};
book.push(booktemp);
booktemp = {
"quantity" : 1
};
book.push(booktemp);
booktemp = {
"amount" : 4
};
book.push(booktemp);
booktemp = {
"title" : "A Book About Nothing"
};
book.push(booktemp);
我在这里尝试的是编写一个通用方法,它将遍历结果列表并能够在将来形成一个条目。
var resultsArray = [];
for( int i = 0; i < array.features.length; i ++)
{
var resultsFeatureArray = [];
for( att in array.features[i].attributes)
{
var temp = {
att : array.features[i].attributes[att]
}
resultsFeatureArray.push(temp);
}
resultsArray.push(resultsFeatureArray);
}
那么我怎样才能使数组与本书代码的第一段相同呢?
添加了我的整个示例代码,注释书数组似乎可以工作,但未注释的部分似乎无法显示行
<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
YAHOO.example.Data = {
bookorders: [
]
}
var bookorders = [];
/*
var book = {
"id" : "po-0167",
"date" : new Date(1980, 2, 24),
"quantity" : 1,
"amount" : 4,
"title" : "A Book About Nothing"
};
*/
var book = [];
var booktemp = {
"id" : "po-0167"
};
book.push(booktemp);
booktemp = {
"date" : new Date(1980, 2, 24)
};
book.push(booktemp);
booktemp = {
"quantity" : 1
};
book.push(booktemp);
booktemp = {
"amount" : 4
};
book.push(booktemp);
booktemp = {
"title" : "A Book About Nothing"
};
book.push(booktemp);
bookorders.push(book);
YAHOO.example.Basic = function() {
var myColumnDefs = [
{key:"id", sortable:true, resizeable:true},
{key:"date", formatter:YAHOO.widget.DataTable.formatDate, sortable:true, sortOptions:{defaultDir:YAHOO.widget.DataTable.CLASS_DESC},resizeable:true},
{key:"quantity", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true, resizeable:true},
{key:"amount", formatter:YAHOO.widget.DataTable.formatCurrency, sortable:true, resizeable:true},
{key:"title", sortable:true, resizeable:true}
];
var myDataSource = new YAHOO.util.DataSource(bookorders);
myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
myDataSource.responseSchema = {
fields: ["id","date","quantity","amount","title"]
};
var myDataTable = new YAHOO.widget.DataTable("basic",
myColumnDefs, myDataSource);
return {
oDS: myDataSource,
oDT: myDataTable
};
}();
});