你怎么能改变这个数组:
[["1","one"], ["2","two"], ["3","three"]]
Run Code Online (Sandbox Code Playgroud)
这个?
["1","one"], ["2","two"], ["3","three"]
Run Code Online (Sandbox Code Playgroud)
对于提供无效的第二版我道歉.这就是我真正想要的:
我想添加["0","zero"]到开头[["1","one"], ["2","two"], ["3","three"]],获取:
[["0","zero"], ["1","one"], ["2","two"], ["3","three"]]
Run Code Online (Sandbox Code Playgroud)
我试过了:
["0","zero"] << [["1","one"], ["2","two"], ["3","three"]]
Run Code Online (Sandbox Code Playgroud)
上面的方法产生了这个,它包含一个我不想要的嵌套:
[["0","zero"], [["1","one"], ["2","two"], ["3","three"]]]
Run Code Online (Sandbox Code Playgroud)
unshift 应该为你做:
a = [["1","one"], ["2","two"], ["3","three"]]
a.unshift(["0", "zero"])
=> [["0", "zero"], ["1", "one"], ["2", "two"], ["3", "three"]]
Run Code Online (Sandbox Code Playgroud)
您可能正在寻找flatten:
返回一个新数组,该数组是该数组的一维展平(递归地)。也就是说,对于数组中的每个元素,将其元素提取到新数组中。如果可选的 level 参数确定要展平的递归级别。
[["1","one"], ["2","two"], ["3","three"]].flatten
Run Code Online (Sandbox Code Playgroud)
这给你:
=> ["1", "one", "2", "two", "3", "three"]
Run Code Online (Sandbox Code Playgroud)