Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!