8

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


});

jonleech
  • 461
  • 1
  • 6
  • 30

2 Answers2

8

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

jonleech
  • 461
  • 1
  • 6
  • 30
3
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 = []

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • @llya okies something new to learn, but i want to know is there a way for me to push each individual field "id", "date", "quantity", "amount", "title" into the book array to make it look like the first var book = { "id" : "po-0167", "date" : new Date(1980, 2, 24), "quantity" : 1, "amount" : 4, "title" : "A Book About Nothing" }; . – jonleech Sep 12 '12 at 07:53
  • am trying to insert into the YUI datatable. i added that code inside my question – jonleech Sep 12 '12 at 07:54
  • Make on my var books -> books[0] and you will get your book – Ilya Sep 12 '12 at 07:57
  • i know mine ain't an array due to your constant reminders, what i am saying is that i tried to change to your code and it still not working. so i am asking you (nicely) how should it be changed. you can edit your answer thanks – jonleech Sep 12 '12 at 08:01
  • I need to input the "id" field each at a time iterating through the features and its values so what i am saying is that i don't have the luxury of creating a booktemp like yours with all 5 fields known. mine can be variable. – jonleech Sep 12 '12 at 08:05
  • If you want itearate each field in youe json object you can do next `for(var key in book){ var bookKey = key; var bookValue = obj[key]; }` – Ilya Sep 12 '12 at 08:14