【发布时间】:2019-07-11 19:57:14
【问题描述】:
我有两个 javascript 对象,我想更改其中一个的结构并将另一个合并到第一个中
我已经使用循环进行绑定,但这似乎不是最好的方法
我的第一个对象叫做“导航栏”
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' } ]
我的第二个叫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 } ]
我想要实现的是将 newvehicles 的对象作为“new-cars”的子对象推入导航栏,并通过导航栏并移动任何 ParentId 不为 null 的东西被移动到那个父对象作为一个孩子
我目前正在使用 for 循环进行一些更改:
for (var i = 0; i < navigation.navbar.length; i++) {
var obj = navigation.navbar[i];
if (obj.Slug == 'new-cars') {
obj.child = newvehicles;
}
}
但我正在寻找这样的输出:
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' } } ]
【问题讨论】:
-
您的代码中导航元素的顺序是否一致/有保证?如果是这样,它就像
navbar[2].child = newvehicles;一样简单 - 如果不是,那么你必须先找到索引,然后分配它...... -
不保证顺序,这就是我使用 for 循环查找索引然后设置子项的原因。我还需要为第二部分做一个 for 循环吗?查找具有 parentID 的所有项目,然后查找具有该 ID 的元素并将其作为子元素插入并从主数组中删除?
-
那么这个解决方案(使用
findIndex)结合我上面的代码,就可以了:stackoverflow.com/q/15997879/870729 -
那些是 JS 对象。 JSON 始终是一个字符串。
标签: javascript merge