So in my JS I have 3 variables like so;
fName = 'Lili, Abraham';
lName = 'Vabiens, Lincoln';
email = 'l@vbiens.com, a@lcln.com'
And I'd like to get an object of the person details like so.
So, in the end, I'd have this:
{ firstName: "Lily", lastName: "Vabiens", fullName: : "Lily Vabiens", email: "l@vbiens.com" },
{ firstName: "Abraham", lastName: "Lincoln", fullName: : "Abraham Lincoln", email: "a@lcln.com" },
What would be the smartest way of going about this?
Now considering the lengths of each attribute of data is same, the following code will work.
First we split the strings into arrays.
fName = 'Lili, Abraham';
lName = 'Vabiens, Lincoln';
email = 'l@vbiens.com, a@lcln.com'
f_name_list = fName.split(', ');
l_name_list = lName.split(', ');
email_list = email.split(', ');
Then we loop and append an object to an array.
let result = [];
for ( let i = 0; i < f_name_list.length; i++ ) {
result.push({
firstName: f_name_list[i],
lastName: l_name_list[i],
fullName: f_name_list[i] + ' ' + l_name_list[i],
email: email_list[i]
});
}