javascript - how to create this type of JSON array dynamically -
hardcoded:
var imageinfocols = [{ "location" : "https://dl.dropboxusercontent.com/u/42060851/imagegrid/1.jpg", "title" : "movie 1" }, { "location" : "https://dl.dropboxusercontent.com/u/42060851/imagegrid/2.jpg", "title" : "movie 2" }, { "location" : "https://dl.dropboxusercontent.com/u/42060851/imagegrid/3.jpg", "title" : "movie 3" } ]; each item has 2 properties, location , title.
i want create object 2 properties , push onto json array. best way using loop?
cheers
maybe help:
var objectarray = []; var field1 = 'location'; // dynamically set field name var field2 = 'title'; // same second field var length = 3; (var = 0; < length; i++) { object = {}; // javascript object properties can acccessed // , set dynamically index notation object[field1] = 'https://dl.dropboxusercontent.com/u/42060851/imagegrid/' + + '.jpg'; object[field2] = 'movie ' + i; objectarray.push(object); }; // json actual json string var json = json.stringify(objectarray); i dynamically setting key names, key values , number of javascript objects. in javascript objects , hash tables 1 in same.
Comments
Post a Comment