-5
{ 'notes[]': [ 'book', 'copy', 'pencil', 'eraser' ] }

I just want to parse these sets. Please, anyone suggest me some best approach

Iron Man
  • 13
  • 1
  • 8

2 Answers2

1

Since you are using an invalid identifier as your key (notes[]), you will need to use bracket syntax to access:

var data = { 'notes[]': [ 'book', 'copy', 'pencil', 'eraser' ] };
console.log(data['notes[]']);

Probably would be better to name the key notes instead of notes[]. Then you can access with dot notation: data.notes

zerkms
  • 249,484
  • 69
  • 436
  • 539
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • 2
    If that's the OP's problem then it's a [duplicate](http://stackoverflow.com/q/12953704/218196) and should be closed as such. – Felix Kling Mar 23 '17 at 21:36
  • "reserved characters" ? – zerkms Mar 23 '17 at 21:36
  • @zerkms reserved characters is probably not the correct nomenclature, what I mean by that is: "Characters that cannot be used in a variable, property or function name". `data.notes[]` will not work – Rob M. Mar 23 '17 at 21:38
  • "variable, property or function name" --- or in short, "identifier". So just call it "a not valid identifier" – zerkms Mar 23 '17 at 21:39
  • Yep, thanks - I will update my answer to be more clear – Rob M. Mar 23 '17 at 21:39
  • Actually sir i am new in node js. I just want to post simple array like ["hello", "world"] from my frontend i.e clientside to server side. I am confused that how to send these array. Sorry for my wrong query. – Iron Man Mar 23 '17 at 21:42
-1

Maybe you want like as below code.

var res = { 'notes': [ 'book', 'copy', 'pencil', 'eraser' ] };
console.log(res.notes[0]); //=> book
console.log(res.notes[1]); //=> copy
console.log(res.notes[2]); //=> pencil
console.log(res.notes[3]); //=> eraser
muratoner
  • 2,316
  • 3
  • 20
  • 32