【问题标题】:How to add Arrays to Strings without flattening them in JS?如何将数组添加到字符串而不在 JS 中展平它们?
【发布时间】:2015-10-15 14:49:28
【问题描述】:

我想做这样的事情:

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

【问题讨论】:

    标签: javascript arrays string flatten


    【解决方案1】:

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

    【讨论】:

    • 嘿,我认为不值得再问一个问题,但是对于对象,那么我们如何对对象做同样的事情呢??
    • 同理JSON.stringify()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2020-10-15
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多