Javascript - transform arrays

how to use Javascript to include this list:

var array = ["no yes", "maybe certainly"];

      

in that:

var array2 = ["no", "yes", "maybe", "certainly"]

      

+3


source to share


3 answers


Best used regex

here if there is a chance of multiple spaces in the array elements.

  • Join array elements by space
  • Extracting nonspatial characters from a string using a regular expression


var array = ["       no yes     ", "     maybe     certainly  "];

var array2 = array.join('').match(/\S+/g);

document.write(array2);
console.log(array2);
      

Run code


+4


source


You can join

all elements of an array, and after split

for an array, so



// var array = ["no yes", "maybe certainly"];
var array = ["no yes     ", "    maybe    certainly"];
var array2 = array.join('').trim().split(/\s+/g);

console.log(array2);
      

Run code


+1


source


var array = ["no yes", "maybe certainly"]

answer = converfunction(array);

function converfunction(array){
return array.toString().replace(',',' ' ).split(' ')
}

      

0


source







All Articles