3

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.

1
  • And can you share the code? Commented Apr 19, 2018 at 9:07

8 Answers 8

13

You can simply use Array#join and 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

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

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

Sign up to request clarification or add additional context in comments.

8 Comments

Perfect. Thank you.
This doesnt work if the array contains (for some reason) 2 spaces together.
@CristianS. I added a fix.
Thanks Cristian S. for pointing out.
@parth I already fixed it.
|
5

Use reduce, concat and 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 );

Comments

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.

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

Comments

2

You can use spread operator in a forEach loop:

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);

Comments

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));

Comments

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)

Comments

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);

Comments

0

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

console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.