2

I have a trouble with NodeJS. I have to send an array of array that is structured like this:

[
   "val1":["one","two","three"],
   "val2":["four","five","six"],
   "val3":["seven","eight","nine"]
]

So, when I try to do res.send, in my browser (Chrome) I show only [].

How can I send my array?

1
  • 3
    That's not a valid JavaScript structure. You can't JSON-encode it as it throws a syntax error.
    – Ram
    Oct 19, 2016 at 14:18

1 Answer 1

3

The send function expects a JavaScript object, which can be marshalled as a valid JSON string. In your case, you have a JavaScript array, with three properties. But JSON arrays support only integer keys (called indices).

So, you might want to change the array to a JavaScript object, like this

res.send({
  "val1": ["one", "two", "three"],
  "val2": ["four", "five", "six"],
  "val3": ["seven", "eight", "nine"]
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.