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

116
Views
How to get object inside array and add it to parent object

I have data like this :

data = {
  data: [
    {
      Email: 'test@yopmail.com',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}

I want to get the first array data inside data and add it to parent object.

this is what I expected :

data = {
  Email: 'test@yopmail.com',
  name: 'dsadas',
  amount: '43.243.434',
  date: '22 Januari 2022',
  data: [
    {
      Email: 'test@yopmail.com',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}

I was trying using map and pushing to object but still no luck

How to solve that ?

Please let me know if you need more information if it's still not enough

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could simply use a spread operator syntax here to individually add all elements of the first array entry to the initial object.

const initialData = {
    data: [
        {
            Email: 'test@yopmail.com',
            name: 'dsadas',
            amount: '43.243.434',
            date: '22 Januari 2022',
        },
        {
            Email: 'test@yopmail.com',
            name: 'akdasd',
            amount: '3.234',
            date: '12 Februari 2022',
         },
         {
            Email: 'test@yopmail.com',
            name: 'dasdasd',
            amount: '234.324',
            date: '10 Februari 2022',
         }
    ]
};

const finalData = { ...initialData.data[0], ...initialData };

about 4 years ago · Juan Pablo Isaza Report

0

If you want to apply the source's properties to target object,
Check Object.assign method.

const yourData = {
  data: [
    {
      Email: 'test@yopmail.com',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
};

Object.assign(yourData, yourData.data[0]);
about 4 years ago · Juan Pablo Isaza Report

0

You can use an iteration too :

var obj = {
  data: [
    {
      Email: 'test@yopmail.com',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: 'test@yopmail.com',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}


Object.getOwnPropertyNames(obj.data[0]).forEach((e) => {
  obj[e] = obj.data[0][e]
})

console.log(obj)

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!