JSON 数组推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12383070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:31:22  来源:igfitidea点击:

JSON array pushing

arraysjson

提问by jonleech

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

我有这个示例数组,用于将条目插入 YUI 数据表

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

so how could i make the array the same as the first segment of the book 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
        };
    }();


});

采纳答案by jonleech

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

我尝试并找到了解决方案,因为 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);

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

这将使其显示在数据表中,通用部分将通过使用 temp[att] = attributes[att]; 进行迭代。

回答by Ilya

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

it's not array.
Array is

它不是数组。
数组是

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

and after manipulation in your example you will get next array

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

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

it's not the same.
You must do next:

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

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 = []and var arr = new Array()are the same Not all browsers works well with var arr = []

附注。
var arr = []并且var arr = new Array()是相同的 并非所有浏览器都能与var arr = []