I would like to return a new object with the updated property names in the nested object.
For example in the fakeData
below, I would like to update all the property names from:
JavaScript
x
7
1
id = Id
2
title = PhaseTitle
3
subTasks = Phases
4
dependencyTest = Dependency
5
fromId = FromId
6
toId = ToId
7
The data that I am trying to update :
JavaScript
1
65
65
1
let fakeData = [
2
{
3
id: 7,
4
title: 'Software validation, research and implementation',
5
dependencyTest: null,
6
subtasks: [
7
{
8
id: 18,
9
title: 'Project Kickoff',
10
dependencyTest: [
11
{
12
id: 100,
13
fromId: 18,
14
toId: 11,
15
},
16
{
17
id: 101,
18
fromId: 18,
19
toId: 19,
20
}
21
]
22
},
23
{
24
id: 11,
25
title: 'Research',
26
dependencyTest: null,
27
subtasks: [
28
{
29
id: 19,
30
title: 'Validation with Customers',
31
dependencyTest: [
32
{
33
id: 200,
34
fromId: 19,
35
toId: 18,
36
},
37
{
38
id: 330,
39
fromId: 19,
40
toId: 12,
41
}
42
]
43
}
44
]
45
},
46
{
47
id: 12,
48
title: 'Design',
49
dependencyTest: null,
50
subtasks: [
51
{
52
id: 22,
53
title: 'UI Design',
54
dependencyTest: [{
55
id: 135,
56
fromId: 18,
57
toId: 19,
58
}]
59
}
60
]
61
}
62
]
63
}
64
];
65
So far I’ve tried to use the recurrsion to do this but the subtask are not updating correctly
JavaScript
1
38
38
1
private test() : void {
2
const updatedObject = [];
3
const recursiveFn = (obj : any ) : void => {
4
for (let i = 0; i < obj.length; i++) {
5
const entity: any = {
6
Id: obj[i].id,
7
Duration: obj[i].duration,
8
PhaseName: obj[i].title,
9
Phases: obj[i].subtasks,
10
Dependency: null
11
};
12
13
const dependency : any = [];
14
15
if (obj[i].dependency && obj[i].dependency.length > 0) {
16
for (const depend of obj[i].dependency) {
17
dependency.push({
18
Id: depend.id.toString(),
19
FromId: depend.fromId.toString(),
20
ToId: depend.toId.toString(),
21
Type: depend.type
22
});
23
}
24
}
25
entity.Dependency = dependency;
26
27
// This doesn't work
28
updatedObject.push(entity);
29
30
if (obj[i]?.subtasks?.length > 0) {
31
recursiveFn(obj[i].subtasks);
32
}
33
}
34
};
35
36
recursiveFn(this.fakeData);
37
}
38
Advertisement
Answer
This is a short solution it return a new array without changing the existing one.
It use recursion and uses the Object.entries and fromEntries functions that do exactly that are perfect for this use case
JavaScript
1
77
77
1
const mapping = {
2
id: 'Id',
3
title: 'PhaseTitle',
4
subtasks: 'Phases',
5
dependencyTest: 'Dependency',
6
fromId: 'FromId',
7
toId: 'ToId'
8
}
9
10
11
const rename = (data) => data.map(d =>
12
Object.fromEntries(Object.entries(d).map(([k, v]) => {
13
return [mapping[k] || k, Array.isArray(v)? rename(v):v]
14
})))
15
16
17
let fakeData = [{
18
id: 7,
19
title: 'Software validation, research and implementation',
20
dependencyTest: null,
21
subtasks: [{
22
id: 18,
23
title: 'Project Kickoff',
24
dependencyTest: [{
25
id: 100,
26
fromId: 18,
27
toId: 11,
28
},
29
{
30
id: 101,
31
fromId: 18,
32
toId: 19,
33
}
34
]
35
},
36
{
37
id: 11,
38
title: 'Research',
39
dependencyTest: null,
40
subtasks: [{
41
id: 19,
42
title: 'Validation with Customers',
43
dependencyTest: [{
44
id: 200,
45
fromId: 19,
46
toId: 18,
47
},
48
{
49
id: 330,
50
fromId: 19,
51
toId: 12,
52
}
53
]
54
}]
55
},
56
{
57
id: 12,
58
title: 'Design',
59
dependencyTest: null,
60
subtasks: [{
61
id: 22,
62
title: 'UI Design',
63
dependencyTest: [{
64
id: 135,
65
fromId: 18,
66
toId: 19,
67
}]
68
}]
69
}
70
]
71
}];
72
73
74
75
76
77
console.log(rename(fakeData))