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

122
Vistas
How to iterate the key value pairs of an object in an array in javascript

I have some data I'd like to display in my front end but its name property is an array of objects, with an object nested in that object. This is how the data is structured in the request:

name: Array [ {…} ]
​​
  0: Object { 0: "E", 1: "r", 2: "i", … }
​​​
    0: "E"
​​​
    1: "r"
​​​
    2: "i"
​​​
    3: "c"
​​​
    4: " "
​​​
    5: "C"
​​​
    6: " "
​​​
    7: "P"
​​​
    8: "e"
​​​
    9: "z"
​​​
    10: "z"
​​​
    11: "u"
​​​
    12: "l"
​​​
    13: "o"
​​​
    _id: "6186b6777a38b7da536a6ffd"
​​​
<prototype>: Object { … }
​​
length: 1

I can't map through it (userData.name[0].map) because its not an array.

Whats the correct way to iterate though an object like this and join each letter together?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I don't know that there's a simpler way, but if you use slice you can do it all in one chained line. You could also create a function, for readability and extension.

const arr = [{
  0: 'E', 1: 'r', 2: 'i', 3: 'c', 4: ' ', 5: 'C', 6: ' ',
  7: 'P', 8: 'e', 9: 'z', 10: 'z', 11: 'u', 12: 'l', 13: 'o',
  _id: '6186b6777a38b7da536a6ffd'
}];

let name = Object.values(arr[0]).slice(0,-1).join('');
console.log(name);

const getFull=(o)=>Object.values(o).slice(0,-1).join('');
console.log(getFull(arr[0]));

Or to transform all:

const arr = [{
  0: 'E', 1: 'r', 2: 'i', 3: 'c', 4: ' ', 5: 'C', 6: ' ',
  7: 'P', 8: 'e', 9: 'z', 10: 'z', 11: 'u', 12: 'l', 13: 'o',
  _id: '6186b6777a38b7da536a6ffd'
},{
  0: 'C', 1: 'h', 2: 'r', 3: 'i', 4: 's', 5: ' ',
  7: 'S', 8: 't', 9: 'r', 10: 'i', 11: 'c', 12: 'k', 13: 'l', 14: 'a', 15: 'n', 16: 'd',
  _id: '6186b6777a38b7da536a6ffd'
}];

const getFull=(o)=>Object.values(o).slice(0,-1).join('');
let buffer = arr.map(a=>getFull(a));
console.log(buffer);

about 4 years ago · Juan Pablo Isaza Denunciar

0

As of right now I'm using this

  const fullName = Object.values(userData.name[0]);
  console.log(fullName);
  fullName.pop();
  const name = fullName.join("");
  console.log(name); 

Which gives me what I'm looking for (// Eric C Pezzulo)

Its not pretty but it works, If anyone has a simpler way to do this or clean this up I'm open to any suggestions.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Something like this may look a little cleaner

const arr = [
  {
    0: 'E',
    1: 'r',
    2: 'i',
    3: 'c',
    4: ' ',
    5: 'C',
    6: ' ',
    7: 'P',
    8: 'e',
    9: 'z',
    10: 'z',
    11: 'u',
    12: 'l',
    13: 'o',
    _id: '6186b6777a38b7da536a6ffd'
  }
];

const fullName = Object.values(arr[0]).slice(0, -1).join('');
console.log(fullName);

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