我不明白为什么这不起作用:
var test = ["String", 300]; var other = test.concat(500); alert(test[2]);
或者
alert(test.length) // 1 not 2
是否有一种方法可以让一个元素添加到现有数组中并使其像上面一样可访问?
concat()返回一个新数组。您想要的是push():
concat()
push()
test.push(500);
演示看区别:
> var test = ["String", 300]; > var other = test.concat(500); > test [ 'String', 300 ] > other [ 'String', 300, 500 ] > test.push(1234) > test [ 'String', 300, 1234 ] > other [ 'String', 300, 500 ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
点击生成二维码
我来说两句