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

139
Views
Getting an object of object property and adding it to the parent object JavaScript

I have an object data with two sub data values, One is array of objects and the second one is object type

I want to just extract a value from every object in array and update that value in its parent object

const payload = {
  data: [
 {
    'name': 'User One,
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'admin',
        id: 88888,
        date: '12-23-1'
    }
},
 {
    'name': 'User Two',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'developer',
        id: 88888,
        date: '12-23-1'
    }
},
{
    'name': 'User Three',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'developer',
        id: 88888,
        date: '12-23-1'
    }
},
{
    'name': 'User Four',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'qa',
        id: 88888,
        date: '12-23-1'
    }
},
],

meta: {
        pagination: {
            total: 5,
            current: 10,
            previous: 2,
            link: 'https://example.com'
        }
    }

    }

        console.log([...payload, payload.data.primaryRole = payload.data.primary_role.name])

Of course its a wrong approach, I want to get the name value from primary_role, and update its parent object with this value, like primaryRole = payload.data[primary_role.name]

I want a data like this

const payload = {
  data: [
 {
    'name': 'User One,
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'admin',
        id: 88888,
        date: '12-23-1'
    },
primaryRole: 'admin'
},
 {
    'name': 'User Two',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'developer',
        id: 88888,
        date: '12-23-1'
    },
primaryRole: 'developer'
},
{
    'name': 'User Three',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'developer',
        id: 88888,
        date: '12-23-1'
    },
primaryRole: 'developer'
},
{
    'name': 'User Four',
    'id': 12345,
    date: '12-23-1',
    primary_role: {
        name: 'qa',
        id: 88888,
        date: '12-23-1'
    },
primaryRole: 'qa'
},
],

meta: {
        pagination: {
            total: 5,
            current: 10,
            previous: 2,
            link: 'https://example.com'
        }
    }

    }

Any comments on this???

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

0

Use a simple loop (taking the liberty to use a less ambiguous target property name):

payload.data.forEach ( po_item => { po_item.primaryRoleName = po_item.primary_role.name; });

In order to judge whether that makes sense in the context of your project, more data is needed. In most general terms, it is usually better to produce data in the shape it is needed than to monkey-patch it.

Full-fledged version:

const payload = {
  data: [
     {
        'name': 'User One',
        'id': 12345,
        date: '12-23-1',
        primary_role: {
            name: 'admin',
            id: 88888,
            date: '12-23-1'
        }
    },
     {
        'name': 'User Two',
        'id': 12345,
        date: '12-23-1',
        primary_role: {
            name: 'developer',
            id: 88888,
            date: '12-23-1'
        }
    },
    {
        'name': 'User Three',
        'id': 12345,
        date: '12-23-1',
        primary_role: {
            name: 'developer',
            id: 88888,
            date: '12-23-1'
        }
    },
    {
        'name': 'User Four',
        'id': 12345,
        date: '12-23-1',
        primary_role: {
            name: 'qa',
            id: 88888,
            date: '12-23-1'
        }
    }
  ]
};


payload.data.forEach ( po_item => { po_item.primaryRoleName = po_item.primary_role.name; });

console.log(payload);

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!