如何使用Javascript打开此列表:
var array = ["no yes", "maybe certainly"];
进入这一个:
var array2 = ["no", "yes", "maybe", "certainly"]
答案 0 :(得分:4)
如果数组元素中有多个空格的可能性,最好在这里使用regex
。
var array = [" no yes ", " maybe certainly "];
var array2 = array.join('').match(/\S+/g);
document.write(array2);
console.log(array2);
答案 1 :(得分:1)
您可以join
数组中的所有元素,然后split
将其添加到数组中,就像这样
// var array = ["no yes", "maybe certainly"];
var array = ["no yes ", " maybe certainly"];
var array2 = array.join('').trim().split(/\s+/g);
console.log(array2);

答案 2 :(得分:0)
var array = ["no yes", "maybe certainly"]
answer = converfunction(array);
function converfunction(array){
return array.toString().replace(',',' ' ).split(' ')
}