0

I have such array in Jquery code:

var marr = new Array();
marr[0] = new Object();
marr[0]["name"] = "Spot 1";
marr[0]["value"] = 20;
marr[1] = new Object();
marr[1]["name"] = "Spot 2";
marr[1]["value"] = 70;

How can I represent this array as a (more or less) user friendly string (to give users an ability to send some values as plugin options). An than parse it back for jquery?

asked Jan 19, 2013 at 11:28
2
  • duplicate? stackoverflow.com/questions/5289403/… Commented Jan 19, 2013 at 11:31
  • or simply dont use an array, use an JSON object Commented Jan 19, 2013 at 11:32

3 Answers 3

3

You could have generated a JSON string from the marr array. It may had given you what you are looking for.

> console.log(JSON.stringify(marr));
[
 {
 "name": "Spot 1",
 "value": 20
 },
 {
 "name": "Spot 2",
 "value": 70
 }
]
answered Jan 19, 2013 at 11:53
Sign up to request clarification or add additional context in comments.

Comments

2

Something like this?

var marr = [ {"name": "Spot 1", "value":20}, {"name": "Spot 2", "value":70} ];

This is same as the code you mentioned above.

answered Jan 19, 2013 at 11:30

2 Comments

But then i use something like var name = marr[i].name to get array values, so i need "titles" for array items...
Updated my answer.. Now marr[1].name will give you Spot 1
1

this will be json representation of array

var marr=[
 {"name":"spot 1","value":20}
 {"name":"spot 2","value":70}
 ]
answered Jan 19, 2013 at 11:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.