'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 1:[1]

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)

Solution 2:[2]

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

Solution 3:[3]

Here is the code:

var data = "Mike Port, Henry Split, Pete Micky";

function parseData(data){
  data = data.split(', ')
  let arr = []
  for(i=0;i<data.length;i++){
    arr.push({firstName:data[i].split(' ')[0].replace(',',''),lastName:data[i].split(' ')[1]})
  }
  return arr
}
console.log(parseData(data))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yosvel Quintero Arguelles
Solution 2
Solution 3 Yaver Javid