我有一个可能包含 1 到 11 个元素的数组。我想删除最后一个元素并将其设为新字符串,然后将数组列表的其余部分设置为集体字符串。以下代码不起作用...如果数组列表只有一个元素,我是否需要包含 if else ? (我知道我这样做,我想知道这是否会导致脚本根本不被执行)
var finaling = checked[checked.length].toString();
checked.splice(checked[checked.length]);
checked.toString();
最佳答案
这是我的做法...
I want to remove the last element...
checked.pop();
...and make it a new string...
checked.push('new string');
// or skip the above `pop()`
checked[checked.length - 1] = 'new string';
...then set the rest of the arraylist to a collective string.
checked = checked + '';
数组的默认 toString()
会使用 ','
连接元素。如果您想使用不同的连接字符,请使用join()
。
关于javascript - 将数组的最后一个元素设置为单独的字符串,然后删除该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051440/