JSON array push

I have this example array for a record that needs to be inserted into a YUI datatable

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

can i 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'm trying here is to write a general method that will iterate over a list of results and be able to form a record 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 can i make the array the same as the first segment of the book code?

added my entire code sample, the comment array works, but the part without commenting, it seems, will not be able to show the lines

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

+4
source share
2 answers

I tried and found a solution for it, since att and the values ​​will be an object after I click 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 mapped to datatable, the common part will simply be repeated using temp [att] = attributes [att];

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

it is not an array.
Array

 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 the following array

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

it is not the same.
You should do the following:

 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 work well with var arr = []

+3
source

Source: https://habr.com/ru/post/1433721/


All Articles