Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda