如何在JS中添加数组到字符串而不使其扁平化



我想这样做:

[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."

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

最新更新