Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report

0

You can do in one line combining String#split() with Array#map() and Destructuring assignment

Code:

const data = 'Mike Port, Henry Split, Pete Micky'
const result = data
  .split(', ')
  .map(s => s.split(' '))
  .map(([firstname, lastname]) => ({ firstname, lastname }))
  
console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

In the long-term it maybe better to not specify the keys in the code. What if you were to add a new property called age to the data? That would mean having to go into into the code and update it to accommodate the change which is a hassle.

This example removes that complication by keeping a track of the keys, and looping over them, and the values, to create a new dataset. No hard-coding of the keys anywhere.

const data = {
  firstname: 'Mike,Henry,Pete',
  lastname: 'Port,Split,Micky',
  age: '10,100,20',
  location: 'France,Germany,Iceland',
  role: 'chef,musician,writer',
  tree: 'larch,maple,oak'
};

// Get the keys and values from the object
const keys = Object.keys(data);
const values = Object.values(data).map(str => str.split(','));

// Temporary array
const out = [];

// Loop over the first array in the values array
for (let i = 0; i < values[0].length; i++) {

  // Initialise an empty object
  // for each nested array
  const obj = {};
  
  // Loop over the keys, create a new object with
  // that key, and the element of the nested value array
  // and merge it with the object
  for (let j = 0; j < keys.length; j++) {
    const prop = { [keys[j]]: values[j][i] };
    Object.assign(obj, prop);
  }

  // Push the object to the array
  out.push(obj);

}

console.log(out);

Additional documentation

  • Object.keys

  • Object.keys

  • Object.assign

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!