str8up7od
str8up7od

Reputation: 348

Splitting an array using .split method javascript

I apologize if this has been seen before but I am here:

var fullName = ["Linus Trovalds "];
var birthYear = [1969];
var myArray = [fullName + birthYear];
console.log(myArray);

And I am trying to declare a variable named splitName, and set it equal to fullName split into two separate objects in an array using the split method. In other words, splitName should equal ["Linus", "Torvalds"] when printed to the console. How should this be written so that split name is printed to the console as mentioned above?

This split method is not working for me.

Any help will be greatly appreciated.

Upvotes: 0

Views: 1246

Answers (2)

danner.tech
danner.tech

Reputation: 109

Or you could have simply split the fullName variable at the [5] mark to separate it into two strings in an array:

So it would be (for those looking for this in the future),

var fullName = ["Linus Trovalds"];
var birthYear = [1969];
var myArray = [fullName + birthYear];

var splitName = fullName.split(fullName[5])
//which would output ["Linus", "Trivolds"];

console.log(splitName)
//I changed this since I'm assuming you want the output to be the new array

Upvotes: 0

forJ
forJ

Reputation: 4617

I don't know how you have tried it but on top of my head I think you might have forgotten that fullName variable is an array.

Try console.log(fullName[0].split(" "));

Upvotes: 1

Related Questions