Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON array pushing

Tags:

json

arrays

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

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


});

like image 649
jonleech Avatar asked Sep 12 '12 07:09

jonleech


People also ask

What does array push () do?

push() The push() method adds one or more elements to the end of an array and returns the new length of the array.

How do you push an element to an array?

JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.

How do I push data into a JSON file?

push(newData); To write this new data to our JSON file, we will use fs. writeFile() which takes the JSON file and data to be added as parameters. Note that we will have to first convert the object back into raw format before writing it.

Can we push function in array?

push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.


1 Answers

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

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

like image 191
jonleech Avatar answered Sep 24 '22 11:09

jonleech