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

139
Vistas
How to insert an object into an array which is inside an object?

Suppose if we are having data like this, how can we use dot notations or anything to update the array(data2) inside the object data1 which is inside the array(data).

data = [
data1:
    {
        a:"1",
        b:"2"
    }
]

    // another array(data2 - below) I am having I have to push that array inside 
    data1 as an object
data2 = [
    {
        c:"3"
    },
    {
        d:"4"
    }
    ]

The response I want as below:

    data = [
data1:
    {
        a:"1",
        b:"2"
    },
data2 = [
    {
        c:"3"
    },
    {
        d:"4"
    }
  ]
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I didnt really understand your question but i think this is what you want?!

data = {
    data1:
    {
        a: "1",
        b: "2"
    }
}
data2 = [
    {
        c: "3"
    },
    {
        a: "3"
    },
    {
        d: "4"
    }
]

for (var key in data2) {
    for (var key2 in data2[key]) {
        if(data.data1[key2] != null){
            console.log("data.data1 with they key" + key2 + " could have been replaced/updated with " + data2[key][key2]);
        }
        console.log("key " + key2 + " has value " + data2[key][key2]);
    }
}

Result:

key c has value 3 data.data1 with they keya could have been replaced/updated with 3 key a has value 3 key d has value 4

Edit:

Why dont you just do

data["data2"] = data2?
about 4 years ago · Juan Pablo Isaza Denunciar

0


var array = [];

array['data1'] = { 'name': 'a' }

var array2 = [{ c: 3 }, { d: 4 }];

array['data2'] = array2;

console.log(array)

OutPut:

[ data1: { name: 'a' }, data2: [ { c: 3 }, { d: 4 } ] ]

about 4 years ago · Juan Pablo Isaza Denunciar

0

I tried on myself and I got the solution. If we have to make another field in an object you just give a name like this

data.dataTwo

this will create another field with new name and now if we want to transfer or store an array in this we can simply do this

data.dataTwo = data2

This will assign that data2 named array in a new field what is given as dataTwo

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