Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda