0

I have an array

var user=[];

user['name']="joy";
user['age']= "12";

is there any function in node.js .So that i can get the json output like

  {"name":"joy","age":"12"}
Linto P D
  • 8,839
  • 7
  • 30
  • 39

2 Answers2

1

No, there is no predefined function in nodejs that will do it for you. But as said by the others, you can do it manually or avoid the problem by defining the array as an objet.

skornous
  • 71
  • 1
  • 5
  • ok. if i defined it as object.And i need to add one more item (sex =>male)dynamically.then how can i do it.can you mention a example – Linto P D May 05 '15 at 10:38
  • 1
    @LintoPD You shoud read [Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) – Vishwanath May 05 '15 at 10:41
  • Just as you added the name and the age. Objects can be used as array in this way (I am going to get yelled at for saying that). For the example, it would be something like `var user=[]; user['name']="joy"; user['age']= "12"; /* later in the code */ user['sexe'] = "male";` – skornous May 05 '15 at 10:46
  • @skornous I yell at you for saying that :P – Vishwanath May 05 '15 at 11:55
0

Thank you for your suggestion.And i got the answer that i want.

var user = {};
user.name = "joy";
user.age =  "12";

JSON.stringify(user)
Linto P D
  • 8,839
  • 7
  • 30
  • 39