I have problem with comparing 2 arrays of objects. I was searching lodash documentation, but I was not able to find proper method. The thing is that I need to compare objects by different keys.
private parentArray: {}[] = [
{ Id: 1, Name: 'A' },
{ Id: 2, Name: 'B' },
{ Id: 3, Name: 'C' },
{ Id: 4, Name: 'D' }
];
private childArray: {}[] = [
{ Id: 2, parentId: 2, Name: 'a' },
{ Id: 3, parentId: 2, Name: 'b' },
{ Id: 4, parentId: 4, Name: 'c' },
{ Id: 5, parentId: 4, Name: 'd' }
];
I need to make a new array of nested objects where 'parentId' will be matched with 'Id' of parent which will look like that:
private newArray = [
{ Id: 1, Name: 'A', Children: [] },
{
Id: 2,
Name: 'B',
Children: [
{ Id: 2, parentId: 2, Name: 'a' },
{ Id: 3, parentId: 2, Name: 'b' }
]
},
{
Id: 3,
Name: 'C',
Children: []
},
{
Id: 4,
Name: 'D',
Children: [
{ Id: 4, parentId: 4, Name: 'c' },
{ Id: 5, parentId: 4, Name: 'd' }
]
}
];
I was using '.intersectionWith([arrays], [comparator])' and '.isMatchWith(object, source, [customizer])' but it did not give me what I need. I will be grateful for any help.