合并键匹配的对象
Merge object where key is matching
我是 typescript 或 node.js 的新手,我正在尝试组合以下 JSON 数组
注意:不确定客户端是否允许任何新的依赖项或插件
这可以在不使用下划线或 jquery extend
的情况下实现吗
[ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ]
到
[ { parentauthscopekey: 1, childauthscopekey: [ 2, 1 ] },
{ parentauthscopekey: 12, childauthscopekey: [ 10, 11, 12 ] },
{ parentauthscopekey: 13, childauthscopekey: [ 1, 13 ] } ]
尝试了下面的代码,它只是行数太多但可以完成工作,但希望在不使用硬编码范围和 scopeMap 值的情况下得到一些简单的东西
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)
你可以:
- 使用
array.reduce
构建预期结构
- 恢复或每个条目与
array.filter
匹配的 parentauthscopekey 的子项
- 并将当前的 parentauthscopekey 添加到 childauthscopekey
var data = [ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ];
var result = data.reduce((acc, current) => {
if (!acc.some(one => one.parentauthscopekey === current.parentauthscopekey)) {
var childs = data.filter(one => one.parentauthscopekey === current.parentauthscopekey).map(one => one.childauthscopekey);
childs.push(current.parentauthscopekey);
acc.push({
parentauthscopekey: current.parentauthscopekey,
childauthscopekey: childs
})
}
return acc;
}, []);
console.log(result);
我是 typescript 或 node.js 的新手,我正在尝试组合以下 JSON 数组 注意:不确定客户端是否允许任何新的依赖项或插件 这可以在不使用下划线或 jquery extend
的情况下实现吗[ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ]
到
[ { parentauthscopekey: 1, childauthscopekey: [ 2, 1 ] },
{ parentauthscopekey: 12, childauthscopekey: [ 10, 11, 12 ] },
{ parentauthscopekey: 13, childauthscopekey: [ 1, 13 ] } ]
尝试了下面的代码,它只是行数太多但可以完成工作,但希望在不使用硬编码范围和 scopeMap 值的情况下得到一些简单的东西
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)
你可以:
- 使用
array.reduce
构建预期结构 - 恢复或每个条目与
array.filter
匹配的 parentauthscopekey 的子项
- 并将当前的 parentauthscopekey 添加到 childauthscopekey
var data = [ { parentauthscopekey: 1, childauthscopekey: 2 },
{ parentauthscopekey: 12, childauthscopekey: 10 },
{ parentauthscopekey: 12, childauthscopekey: 11 },
{ parentauthscopekey: 13, childauthscopekey: 1 } ];
var result = data.reduce((acc, current) => {
if (!acc.some(one => one.parentauthscopekey === current.parentauthscopekey)) {
var childs = data.filter(one => one.parentauthscopekey === current.parentauthscopekey).map(one => one.childauthscopekey);
childs.push(current.parentauthscopekey);
acc.push({
parentauthscopekey: current.parentauthscopekey,
childauthscopekey: childs
})
}
return acc;
}, []);
console.log(result);