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

203
Vistas
add multiple objects to an array of objects at a certain index

I have an array that looks similar to this,

[
    { 'name': 'Bob', 'age': 25, 'sex': 'M' },
    { 'name': 'Gill', 'age': 33, 'sex': 'F' },
    { 'name': 'Glen', 'age': 55, 'sex': 'M' },
    { 'name': 'Flo', 'age': 19, 'sex': 'F' }
]

I have another array of objects that looks like this,

[
    { 'name': 'Fred', 'age': 49, 'sex': 'M' },
    { 'name': 'Thelma', 'age': 27, 'sex': 'F' }
]

I want to add this second of objects to my first array so the first array would look like this,

[
    { 'name': 'Bob', 'age': 25, 'sex': 'M' },
    { 'name': 'Fred', 'age': 49, 'sex': 'M' },
    { 'name': 'Thelma', 'age': 27, 'sex': 'F' }
    { 'name': 'Gill', 'age': 33, 'sex': 'F' },
    { 'name': 'Glen', 'age': 55, 'sex': 'M' },
    { 'name': 'Flo', 'age': 19, 'sex': 'F' }
]

So I thought it would be case of doing,

originalPeople.splice(1, 0, newPeople)

But when I console.log my originalPeople array doing this, I get the following similar output,

{ 'name': 'Bob', 'age': 25, 'sex': 'M' },
    ƒ newPeople(),
    { 'name': 'Gill', 'age': 33, 'sex': 'F' },
    { 'name': 'Glen', 'age': 55, 'sex': 'M' },
    { 'name': 'Flo', 'age': 19, 'sex': 'F' }

I assume you can splice an array objects into an array and I have to do something different?

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

0

Use spread operator to add newPeople (see code below)

let originalPeople = [
    { 'name': 'Bob', 'age': 25, 'sex': 'M' },
    { 'name': 'Gill', 'age': 33, 'sex': 'F' },
    { 'name': 'Glen', 'age': 55, 'sex': 'M' },
    { 'name': 'Flo', 'age': 19, 'sex': 'F' }
]

let newPeople = [
    { 'name': 'Fred', 'age': 49, 'sex': 'M' },
    { 'name': 'Thelma', 'age': 27, 'sex': 'F' }
]

originalPeople.splice(1, 0, ...newPeople)

console.log(originalPeople)
about 4 years ago · Juan Pablo Isaza Denunciar

0

could this be useful?

var arr1 = [
    { 'name': 'Bob', 'age': 25, 'sex': 'M' },
    { 'name': 'Gill', 'age': 33, 'sex': 'F' },
    { 'name': 'Glen', 'age': 55, 'sex': 'M' },
    { 'name': 'Flo', 'age': 19, 'sex': 'F' }
]

var arr2 = [
    { 'name': 'Fred', 'age': 49, 'sex': 'M' },
    { 'name': 'Thelma', 'age': 27, 'sex': 'F' }
]

arr1.push(...arr2);

console.log(arr1)

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