Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return object of first and last names

The result is returning {firstname: ‘Mike,Henry,Pete’, lastname: ‘Port,Split,Micky’}
I want result to return
{
firstName: Mike,
lastName: Port
},
{
firstName: Henry,
lastName: Split
},
{
firstName: Pete,
lastName: Micky
}

var data = "Mike Port, Henry Split, Pete Micky";
var firstName = [];
var lastName = [];
var result = [];
var newNames = data.replaceAll(", ", ",");
var fullName = newNames.split(',');
fullName.forEach(name => {
  let splitted = name.split(" ");
  firstName.push(splitted[0]);
  lastName.push(splitted[1]);
});

f_name_list = firstName.toString().split(", ");
l_name_list = lastName.toString().split(", ");

for (let i = 0; i < f_name_list.length; i++) {
  result.push({ 
    firstname: f_name_list[i],
    lastname: l_name_list[i]
  });
}

console.log(result);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

split data with ", " then split with " "

var data = "Mike Port, Henry Split, Pete Micky";
var result = [];
var newNames = data.split(", ");
newNames.forEach(name => {
  let splitted = name.split(" ");
  result.push({ 
    firstname:splitted[0],
    lastname: splitted[1]
  });
});

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading