user663724
user663724

Reputation:

Construct a Javascript Template and inserting values into it

If anybody knows , please help me , i am very badly struck .

From AJAX Call i am constructing this data in my server (This data is fetched from the server so there can be any number of such rows data )

{data:[{one:"1",two:"2"},{one:"3",two:"3"}]}

My question is that , is it possible to construct a similar array inside javascript dynamically ??

For example , depending upon the number of rows , i want to construct a similar jaavscript array dynamically (For example depneding on data.length , i want to construct this type dynamically

var data = {
  jobs:[
    {one:"1",two:"2"},
    {one:"3",two:"3"}
  ]
};

Please help me .

Upvotes: 0

Views: 76

Answers (1)

Blazes
Blazes

Reputation: 4779

This will dynamically create a list of dictionary, which is what I think you are looking for:

var row = {};
row['one'] = "1";
row['two'] = "2";
data.push(row);
row = {};
row['one'] = "3";
row['two'] = "3";
data.push(row);
// outputs [{"one":"1","two":"2"},{"one":"3","two":"3"}]
alert( JSON.stringify( data ) )

Upvotes: 1

Related Questions