如何将数组添加到字符串而不在JS中展平它们?

Div*_*ham 2 javascript arrays string flatten

我想做这样的事情:

[1,[2],[3,4]] + " is a Nested Array." => "[1,[2],[3,4]] is a Nested Array."

但是, console.log([1,[2],[3,4]] + " is a Nested Array."); 给出1,2,3,4 is a Nested Array.

我可以通过在字符串中添加方括号并使数组变平来实现单维数组:

 "["+ [1,2,3,4,5] + "] is a single dimensional array." 
      => "[1,2,3,4,5] is a single dimensional array."
Run Code Online (Sandbox Code Playgroud)

dep*_*erm 8

console.log(JSON.stringify([1,[2],[3,4]]) + " is a Nested Array.");
Run Code Online (Sandbox Code Playgroud)