Reputation: 1505
Using Javascript (preferably in a for loop) How can I covert an array of full names into objects so that I can split those objects into key value pairs with the keys being called "first" & "last", and the values be the full name split up into those categories?
I started out with a single string of names that were separated onto new lines...So far I have split this string into an array by using the .split("\n") method. I now have excess white space in the front of each name...not sure if that really matters or not...but I have tried .splice(" ") to try to get rid of the excess white space... but it didn't work. As you can see I need a little help. Thank you!
var nameString = document.getElementsByTagName("textarea")[0].value;
var nameArray = nameString.split("\n");
Here are the results of the split string:
[" Genevieve Harber IV",
" Dewitt Weber",
" Krystina O'Kon",
" Jeremy Adams",
" Aileen Lakin",
" Jesus Quigley",
" Shea Rempel",
" Citlalli Bernier",
" Forest Dare",
" Reina Kilback",
" Nathanael Abshire",
" Augustus Hoppe",
" Myrtis Herzog",
" Jack Senger",
" Ronny Smitham",
" Jordi Bruen",
" Leta Pfannerstill",
" Kayla Kshlerin",
" Norbert Thompson",
" Nat Lebsack"]
Upvotes: 1
Views: 106
Reputation: 147523
If you need to cope with last names of more than one word (e.g. Van Damme), then the following will help. It treats the first word as the first name and the rest as the last name. It also handles names that are just one word (e.g. "Sting") by making it the first name and "" (empty string) for the last name. The callback function could be modified to reverse that if required.
It also removes additional whitespace without any extra processing.
var data =
[" Genevieve Harber IV",
" Dewitt Weber",
" Krystina O'Kon",
" Jean-Claude Van Damme",
" Giessel"];
var names = data.reduce(function(names, name) {
name = name.match(/\S+/g);
if (name) {
names.push({first: name.shift(), last: name.join(' ')});
}
return names;
}, []);
console.log(JSON.stringify(names));
// [{"first":"Genevieve","last":"Harber IV"},
// {"first":"Dewitt","last":"Weber"},
// {"first":"Krystina","last":"O'Kon"},
// {"first":"Jean-Claude","last":"Van Damme"},
// {"first":"Giessel","last":""}]
However, names might be more complex than that. The good part of using reduce is that you can eliminate records that don't pass validation, e.g. if there's an empty string, the above won't put it into the names array.
Upvotes: 0
Reputation: 11633
var objectList = []; //holder for the array of names
var mylist = [" Genevieve Harber IV",
" Dewitt Weber",
" Krystina O'Kon",
" Jeremy Adams",
" Aileen Lakin",
" Jesus Quigley",
" Shea Rempel",
" Citlalli Bernier",
" Forest Dare",
" Reina Kilback",
" Nathanael Abshire",
" Augustus Hoppe",
" Myrtis Herzog",
" Jack Senger",
" Ronny Smitham",
" Jordi Bruen",
" Leta Pfannerstill",
" Kayla Kshlerin",
" Norbert Thompson",
" Nat Lebsack"];
//loop over the list
for (var i=0; i < mylist.length; i++) {
var trimmed=mylist[i].trim(); //trim excess spaces
var names = trimmed.split(" "); //split on spaces in between
objectList.push({"first": names[0], "last": names[1]}); //push object onto array
}
//print the 4th name for demonstration of how to access it
console.log(objectList[3].last + ", " + objectList[3].first);
Upvotes: 1
Reputation: 174
Use the map function (Array.prototype.map)
nameArray.map(function (fullName) {
var names = fullName.trim().split(' '); // takes care of excess whitespace
return {
first: names[0],
last: names[1]
};
});
This will give you an array of objects, where each object has a 'first' and 'last' property corresponding to the first/last name.
Upvotes: 4
Reputation: 198506
If you know you will always have space in front, and there will always be two names:
var peopleArray = nameArray.map(function(n) {
o = n.split(/\s+/); return { first: o[1], last: o[2] };
});
Upvotes: 1