Aleixs Stacks Aleixs Stacks - 1 month ago 9
Node.js Question

NodeJS send array of array

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?

Answer

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"]
});