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???
CodePudding user response:
use Array.map
const paylaodDataWithRoleName = payload.data.map(item => ({
...item,
primaryRoleName: item.primary_role?.name
}))
CodePudding user response:
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);
CodePudding user response:
You can simply achieve this requirement dynamically by just using Array.forEach()
method.
Live Demo :
// Input payload 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'
}
}
};
// Iterating over data array and assiging the child property values to parent.
payload.data.forEach(obj => {
Object.keys(obj).forEach(key => {
if (typeof obj[key] !== 'object') {
obj[key] = obj.primary_role[key]
}
})
});
// Output
console.log(payload);