标签: javascript arrays immutability
这很奇怪,但我找不到任何简单的解决方案。
注意:我使用不变性,所以我绝对不想修改原始数组(不建议使用push)
push
[].concat(["x","y"],"z") - >如何获取[["x","y"],"z"]而不是["x","y","z"]?
[].concat(["x","y"],"z")
[["x","y"],"z"]
["x","y","z"]
答案 0 :(得分:8)
您可以使用嵌套数组:
> [].concat([["x", "y"]], "z") [["x", "y"], "z"]