JSON阵列推 [英] JSON array pushing

查看:112
本文介绍了JSON阵列推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个条目这个例子阵列插入到数据表YUI

I have this example array for an entry to be inserted to a YUI datatable

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

我将能够通过这样做是为了得到相同的阵列?

will i be able to get the same array by doing this?

    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);

什么,我想在这里是编写将通过结果的列表,并能重复,以形成对未来的条目的一般方法。

what i am trying here is to write a generic method that will iterate through a list of results and able to form an entry in the future.

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);
}

所以,我怎么可以使阵列一样的书code的第一段?

so how could i make the array the same as the first segment of the book code?

加了我整个样本code,注释的书阵似乎工作,但注释掉的部分似乎不能显示行

added my whole sample code, the commented book array seems to work but the uncommented part seems to not be able to show the rows

<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
        };
    }();


});

推荐答案

我试过,发现解决它,因为AT&T和值将对象后,我推

I tried and found the solution to it, since the att and values will be object after i push it

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);

这将使其在数据表显示,通用部分将只是通过使用临时[ATT]迭代=属性[ATT];

this will make it display in the datatable, the generic part will just be iterated through using the temp[att] = attributes[att];

这篇关于JSON阵列推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆