如何将数组添加到字符串而不在 JS 中展平它们?
How to add Arrays to Strings without flattening them in 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.");
我想做这样的事情:
[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.");