I've got two javascript objects, i want to change the structure of one of them and merge the other one into the first one
I've tied using loops but it doesn't seem to be the best way to do it
My first object is called 'navbar'
navbar:
[ { Id: 7,
ParentId: null,
Name: 'Home',
Slug: '/',
Priority: 1,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom Plus' },
{ Id: 15,
ParentId: 14,
Name: 'About Us',
Slug: 'about-us',
Priority: 1,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom Plus' },
{ Id: 8,
ParentId: null,
Name: 'New Cars',
Slug: 'new-cars',
Priority: 2,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom' },
{ Id: 14,
ParentId: null,
Name: 'More',
Slug: 'more',
Priority: 8,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Placeholder' } ]
And my second one is called newvehicles:
newvehicles:
[ { Id: 5,
VehicleType: 'Car',
MakeId: 5,
MakeName: 'Make',
MakeSlug: 'make',
ModelId: 13,
ModelName: 'All-New Car',
ModelSlug: 'all-new-car',
Prefix: null,
Suffix: null,
Priority: 1,
Redirect: null,
Image:
'http://xxxx.jpg',
Assets: null,
Markup: null },
{ Id: 6,
VehicleType: 'Car',
MakeId: 5,
MakeName: 'Car',
MakeSlug: 'Car',
ModelId: 8,
ModelName: 'Car',
ModelSlug: 'Car',
Prefix: null,
Suffix: null,
Priority: 2,
Redirect: null,
Image:
'http://xxxx.jpg',
Assets: null,
Markup: null } ]
What i want to achieve is for the object of newvehicles to be pushed into the navbar as a child of 'new-cars' and also to go through the navbar and move anything that has a ParentId of not null to be moved into that parent object as a child
I'm currently using a for loop to do some of the changes:
for (var i = 0; i < navigation.navbar.length; i++) {
var obj = navigation.navbar[i];
if (obj.Slug == 'new-cars') {
obj.child = newvehicles;
}
}
But i'm looking for the output to be something like this:
navbar:
[ { Id: 7,
ParentId: null,
Name: 'Home',
Slug: '/',
Priority: 1,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom Plus' },
{ Id: 8,
ParentId: null,
Name: 'New Cars',
Slug: 'new-cars',
Priority: 2,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom'.
child : [ { Id: 5,
VehicleType: 'Car',
MakeId: 5,
MakeName: 'Make',
MakeSlug: 'make',
ModelId: 13,
ModelName: 'All-New Car',
ModelSlug: 'all-new-car',
Prefix: null,
Suffix: null,
Priority: 1,
Redirect: null,
Image:
'http://xxxx.jpg',
Assets: null,
Markup: null },
{ Id: 6,
VehicleType: 'Car',
MakeId: 5,
MakeName: 'Car',
MakeSlug: 'Car',
ModelId: 8,
ModelName: 'Car',
ModelSlug: 'Car',
Prefix: null,
Suffix: null,
Priority: 2,
Redirect: null,
Image:
'http://xxxx.jpg',
Assets: null,
Markup: null } ] },
{ Id: 14,
ParentId: null,
Name: 'More',
Slug: 'more',
Priority: 8,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Placeholder',
child: { Id: 15,
ParentId: 14,
Name: 'About Us',
Slug: 'about-us',
Priority: 1,
MenuDisplay: true,
MenuSubDisplay: false,
MobileDisplay: false,
PageType: 'Custom Plus' } } ]