我有两个 javascript 对象,我想更改其中一个的结构并将另一个合并到第一个中
我已经使用循环绑定(bind)了,但这似乎不是最好的方法
我的第一个对象叫做'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' } ]
我的第二个叫做 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 不为空的对象移动到该父对象作为子对象
我目前正在使用 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.find(item => item.Slug == 'new-cars').child = newvehicles;
对于非null的parentId,可以:
- 通过外部数组向后循环,
- 在循环中,找到任何具有与当前项的 parentId 匹配的 id 的数组项,
- 拼出当前项目( child )
- 将其加载到检索到的父级的“子级”属性中,
代码如下:
for (var i = navbar.length - 1; i >= 0; i--) {
var obj = navbar[i];
let parent = navbar.find(item => item.Id == obj.ParentId);
if (parent) {
parent.child = navbar.splice(i,1);
}
}
在这两种情况下,前缀“导航”。必要时“导航栏”和/或“新车辆”。
编辑:处理 OP 在下面请求将多个 child 与 parent 关联的能力的评论。
为了让“child”属性允许多个 child ,我们将把它变成一个数组。所以我们首先检查父级中是否存在“子级”属性。如果不是,我们创建它并将其设置为一个空数组。然后,我们将拼接后的子对象压入循环中:
for (var i = navbar.length - 1; i >= 0; i--) {
var obj = navbar[i];
let parent = navbar.find(item => item.Id == obj.ParentId);
if (parent) {
if (!parent.child)
parent.child = [];
parent.child.push(navbar.splice(i,1)[0]);
}
}
考虑根据您的需要将“child”的名称更改为“children”。
另外,请注意,所有这些仅适用于一个级别。如果您需要 parent 到 child 的多层嵌套,那么简单的循环是不够的。在这种情况下,您可能想创建一个不同的问题。
关于javascript - 合并和嵌套 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996460/