7

我有这个示例数组,用于将条目插入到 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
        };
    }();


});

4

2 回答 2

7

我尝试并找到了解决方案,因为 att 和 values 在我推送后将成为对象

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] = attributes[att]; 进行迭代

于 2012-09-12T09:23:54.867 回答
3
var book = {
        "id" : "po-0167",
        "date" : new Date(1980, 2, 24),
        "quantity" : 1,
        "amount" : 4,
        "title" : "A Book About Nothing"
    };  

它不是数组。
数组是

var books =[{
   "id" : "po-0167",
   "date" : new Date(1980, 2, 24),
   "quantity" : 1,
   "amount" : 4,
   "title" : "A Book About Nothing"
}]  

在您的示例中进行操作后,您将获得下一个数组

var book2 =[{
   "id" : "po-0167"
},{
   "date" : new Date(1980, 2, 24)
   },{
   "quantity" : 1
},{
   "amount" : 4
},{
   "title" : "A Book About Nothing"
}]  

这是不一样的。
您必须执行以下操作:

var book = new Array();

var booktemp = {
   "id" : "po-0167",
   "date" : new Date(1980, 2, 24),
   "quantity" : 1,
   "amount" : 4,
   "title" : "A Book About Nothing"
};  

book.push(booktemp);  

PS。
var arr = []并且var arr = new Array()是相同的并非所有浏览器都适用于var arr = []

于 2012-09-12T07:43:12.077 回答