split () method javascript in an Array

-2

How do you get the same result as the variable below in the array ?

// Variável
var str = "How are you doing today?";
var res = str.split(" ", 3);
document.write(res)

// Array
var str = ["How are you doing today?"];
var res = str.split(" ", 3);
document.write(res)
    
asked by anonymous 28.09.2017 / 15:06

2 answers

0

// Utilizando for
var phrases = ["How are you doing today?", "I am great"];
for (var index in phrases) {
  console.log(phrases[index].split(" ", 3));
}

// Utilizando map
phrases = ["How are you doing today?", "I am great"];
var results = phrases.map(function(element, index, array) {
  return element.split(" ", 3);
});

console.log(results);
console.log(results[0]);
console.log(results[1]);
    
28.09.2017 / 20:13
0

I was able to do this:

// Variável
var str = "How are you doing today?";
var res = str.split(" ", 3);
console.log(res)

// Array
var str = ["How are you doing today?"];
for(var i = 0; i < str.length; i++){
	var res = str[i].split(" ", 3)
console.log(res)
}
    
28.09.2017 / 21:13