JavaScript Setting up a JSON array for later use -
I want to create a JSON array, create fields and later populate it with the data in my code. I'm just wondering what the syntax has to go about this. I have tried so far, but there are some parse errors;
& lt; Script & gt; Var myJSONArray = [{items :, cost: quantity:}; // to prepare populated array for population] then later ....
myJSONArray.push (milk, 1.99,2) ; // Then change item in array areas
If you really want to create one You can use the JSON.stringify method, JSON array (you currently use a JavaScript ARM in which there is a javascript object). Using Andy's Code:
var arr = []; Arr.push ({item: 'milk', cost: 1.99, quantity: 2}); // prints the JavaScript array as a JSON array: console.log (JSON.stringify (arr)); The function will return a JavaScript value string in JSON format. You can then parse it with another language or store it in a new .json file. If this was the purpose of you.
UPDATE : Output of the previous code: '[["item": "milk", "cost": 1.99, "quantity ": 2}] '
Comments
Post a Comment