JSON数组推送

时间:2012-09-12 07:22:46

标签: arrays json

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


});

2 个答案:

答案 0 :(得分:5)

我试过并找到了它的解决方案,因为在我推动它之后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] = attributes [att];

进行迭代

答案 1 :(得分: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 = []