Split() javascript method in an array

Asked

Viewed 174 times

-2

How to achieve the same result of variable down 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)

  • 1

    make a loop of str and Voce can give a split... str[i].split(" ", 3)

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

0

I got it this way:

// 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)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.