0

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...

2
  • 1
    So, what do you want? Create an object and stringify it? Or vice versa, get a string and then build an object from it? Commented Aug 17, 2011 at 11:53
  • @Saurabh: from where are you getting this product_id and name. thats what we would like to know Commented Aug 17, 2011 at 12:05

1 Answer 1

3
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] };
}
Sign up to request clarification or add additional context in comments.

4 Comments

how can i create this object if i have product ids and product name..?
@Saurabh: where is your product_id and name now?
How i create this object---- [ { product_id : 123, name : 'stack' }, { product_id : 456, name : 'overflow' } ];
@Saurabh Kumar: Well, he did write how to create it. Just copy any of 2 example codes and use it. Dont forget to accept the answer (press on big green "V" button)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.