Saurabh Kumar
Saurabh Kumar

Reputation: 16651

How to create a string in javascript which i can Stringify()

I have a requirement like as follows :

I have productid and productname and then I want to create a object which on calling JSON.Stringify

showuld look like following

"[{"product_id":"123","name":"stack"},{"product_id":"456","name":"overflow"}]"

Is it possible to do in javascript...

Upvotes: 0

Views: 60

Answers (1)

Thilo
Thilo

Reputation: 262494

var theObject = [ { product_id : 123, name : 'stack' }, 
                  { product_id : 456, name : 'overflow' } ];

or if you have the product ids and names in two arrays:

var theObject = [ ];

for (var i=0; i<2; i++){
    theObject[i] = { product_id : product_ids[i], name : names[i] };
}

Upvotes: 3

Related Questions