如何在Javascript中将带有空格的字符串转换为单个数组列表? [重复]

时间:2022-10-31 13:20:26

This question already has an answer here:

这个问题在这里已有答案:

I have one array as below

我有一个数组如下

var arr = ["john doe first", "robert way", "jason doe", "alex"];

Now i want to convert this array like below.

现在我想转换这个数组,如下所示。

var result = ["john", "doe", "first", "robert", "way", "jason", 
"doe", "alex"];

Quick explanation is i need convert each array element of string into array and reset it into the array. Array index is not important. I tried arr.split(" ") even arr.toString() and then arr.split(" "). I don't know what i'm missing here.

快速解释是我需要将字符串的每个数组元素转换为数组并将其重置为数组。数组索引并不重要。我试过arr.split(“”)甚至是arr.toString()然后是arr.split(“”)。我不知道我在这里缺少什么。

8 个解决方案

#1


11  

You can simply use Array#join and String#split :

您可以简单地使用Array#join和String#split:

var arr = ["john doe first", "robert way", "jason doe", "alex"];

console.log(arr.join(' ').split(' '));

If your Strings have more than one space separating your words :

如果您的字符串有多个空格分隔您的单词:

Add .filter(Boolean) to remove the empty Strings

添加.filter(布尔值)以删除空字符串

var arr = ["john doe    first", "robert way", "jason doe", "alex"];

console.log(arr.join(' ').split(' ').filter(Boolean));

#2


5  

Use reduce, concat and split

使用reduce,concat和split

arr.reduce( (a , c) => a.concat(c.split(/\s+/)), [])

Demo

演示

var output = ["john doe first", "robert way", "jason doe", "alex"].reduce( (a , c) => a.concat(c.split(/\s+/)), []);
console.log( output );

#3


2  

You could use concat method with spread syntax ... to add to a new array and map and split methods to split each element on white space.

您可以使用带扩展语法的concat方法...添加到新数组并映射和拆分方法以拆分空白处的每个元素。

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var result = [].concat(...arr.map(e => e.split(' ')));
console.log(result)

#4


2  

You can use spread operator in a forEach loop:

您可以在forEach循环中使用spread运算符:

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var res = [];
arr.forEach((item)=>{ 
   var tempArray = item.split(' ');
   res.push(...tempArray);
});

console.log(res);

#5


1  

var arr = ["john doe first", "robert way", "jason doe", "alex"];

function formatArray(array){
  var newArr=[];
  array.forEach(function(item){
    item.split(' ').forEach(function(itemSplitted){
      newArr.push(itemSplitted);
    });
  });
  return newArr;
}

console.log(formatArray(arr));

#6


1  

Use this:

用这个:

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var finalArray = []
for(var i = 0; i < arr.length; i ++) {
  arr[i].split(' ').forEach(function(item) {finalArray.push(item);});
}

console.log(finalArray)

#7


1  

var arr = ["john dvoe first", "robert way", "jason doe", "alex"]; 
var arr2= arr.map(function(item){return item.split(" ")});
var result=[].concat.apply([],arr2);

#8


0  

const arr = ["john doe first", "robert way", "jason doe", "alex"];
let result = []
arr.map((e) => {
  result = result.concat(e.split(/[ ,]+/))
})

console.log(result);

#1


11  

You can simply use Array#join and String#split :

您可以简单地使用Array#join和String#split:

var arr = ["john doe first", "robert way", "jason doe", "alex"];

console.log(arr.join(' ').split(' '));

If your Strings have more than one space separating your words :

如果您的字符串有多个空格分隔您的单词:

Add .filter(Boolean) to remove the empty Strings

添加.filter(布尔值)以删除空字符串

var arr = ["john doe    first", "robert way", "jason doe", "alex"];

console.log(arr.join(' ').split(' ').filter(Boolean));

#2


5  

Use reduce, concat and split

使用reduce,concat和split

arr.reduce( (a , c) => a.concat(c.split(/\s+/)), [])

Demo

演示

var output = ["john doe first", "robert way", "jason doe", "alex"].reduce( (a , c) => a.concat(c.split(/\s+/)), []);
console.log( output );

#3


2  

You could use concat method with spread syntax ... to add to a new array and map and split methods to split each element on white space.

您可以使用带扩展语法的concat方法...添加到新数组并映射和拆分方法以拆分空白处的每个元素。

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var result = [].concat(...arr.map(e => e.split(' ')));
console.log(result)

#4


2  

You can use spread operator in a forEach loop:

您可以在forEach循环中使用spread运算符:

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var res = [];
arr.forEach((item)=>{ 
   var tempArray = item.split(' ');
   res.push(...tempArray);
});

console.log(res);

#5


1  

var arr = ["john doe first", "robert way", "jason doe", "alex"];

function formatArray(array){
  var newArr=[];
  array.forEach(function(item){
    item.split(' ').forEach(function(itemSplitted){
      newArr.push(itemSplitted);
    });
  });
  return newArr;
}

console.log(formatArray(arr));

#6


1  

Use this:

用这个:

var arr = ["john doe first", "robert way", "jason doe", "alex"];
var finalArray = []
for(var i = 0; i < arr.length; i ++) {
  arr[i].split(' ').forEach(function(item) {finalArray.push(item);});
}

console.log(finalArray)

#7


1  

var arr = ["john dvoe first", "robert way", "jason doe", "alex"]; 
var arr2= arr.map(function(item){return item.split(" ")});
var result=[].concat.apply([],arr2);

#8


0  

const arr = ["john doe first", "robert way", "jason doe", "alex"];
let result = []
arr.map((e) => {
  result = result.concat(e.split(/[ ,]+/))
})

console.log(result);