我有一个示例数组,用于将条目插入到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 }; }(); });
我尝试并找到了解决方案,因为在我按入后,att和值将成为对象
var temp = new Object(); temp["id"] = "po-0167"; temp["date"] = new Date(1980, 2, 24); temp["quantity"] = 1; temp["amount"] = 4; temp["title"] = "A Book About Nothing"; bookorders.push(temp);
这将使其显示在数据表中,而通用部分将通过使用temp [att] = attribute [att]进行迭代;