I am unfamiliar with typescript and node.js, and I am attempting to merge the JSON array below. Please note that I am unsure if the client allows for new dependencies or plugins. Is it possible to achieve this without using underscore or jQuery extend?
[ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ]
Desired Output:
[ { parentauthscopekey: 1, childauthscopekey: [ 2, 1 ] },
{ parentauthscopekey: 12, childauthscopekey: [ 10, 11, 12 ] },
{ parentauthscopekey: 13, childauthscopekey: [ 1, 13 ] } ]
I have attempted the code below, it works but it is lengthy. I am hoping for a simpler solution without manually specifying scope and scopeMap values.
table = [ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ]
scope=[1,12,13]
scopeMap = new Set()
table2 = []
scope.forEach(parentauthscopekey => {
table.forEach(row =>{
if(row.parentauthscopekey == parentauthscopekey && !scopeMap.has(parentauthscopekey))
{
scopeMap.add(parentauthscopekey)
childauthscopekey = []
table.filter(c => c.parentauthscopekey == row.parentauthscopekey).forEach(r=>{
childauthscopekey.push(r.childauthscopekey)
})
childauthscopekey.push(parentauthscopekey)
table2.push({parentauthscopekey, childauthscopekey})
}
})
})
console.log(table2)