我有3组对象,其中1组可以通过唯一键映射到相应的组。可以通过不同的键将同一组映射到第三组。我需要能够将所有这些映射到一个新的组合集中。这些集合都具有不同的属性。
唯一计数[用户] [部门]
许多发票[p]
每个唯一的[用户]都属于(comitID)找到的特定[部门],但是同一[用户]可以具有许多[发票]。如果您熟悉关系数据库,则为一对多字段
const users= [ // Unique Entries
{name:'user1', comitId: 'aa1'},
{name:'user2', comitId: 'aa2'}
]
const sector= [ // Unique Entries
{comitID: 'aa1', department: 'finance'},
{comitID: 'aa2', department: 'marketing'},
{comitID: 'aa3', department: 'engineering'}
]
const invoices= [ // Multiple Entries
{name: 'user1' : statementDate: '2/1/2019'},
{name: 'user1' : statementDate: '2/14/2019'},
{name: 'user2' : statementDate: '2/1/2019'}
]
新设置应如下所示。无法包含对帐单日期的列表,它们每个都需要是一个新对象。
const results = [
{name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/1/2019'},
{name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/14/2019'},
{name: 'user2', comitId: 'aa2', department: 'marketing', statementDate: '2/1/2019'}
]
我一直在Excel中使用vlookups和公式进行尝试。这些文件的不重复计数通常为10k,发票为40k。
答案 0 :(得分:1)
您可以在invoices
和Array.map()
上使用Array.find()
,以在users
然后sectors
中获得相应的条目:
const users = [ // Unique Entries
{name:'user1', comitId: 'aa1'},
{name:'user2', comitId: 'aa2'}
];
const sectors = [ // Unique Entries
{comitID: 'aa1', department: 'finance'},
{comitID: 'aa2', department: 'marketing'},
{comitID: 'aa3', department: 'engineering'}
];
const invoices = [ // Multiple Entries
{name: 'user1', statementDate: '2/1/2019'},
{name: 'user1', statementDate: '2/14/2019'},
{name: 'user2', statementDate: '2/1/2019'}
];
const result = invoices.map(invoice => {
const user = users.find(u => u.name === invoice.name) || {};
const sector = sectors.find(s => s.comitID === user.comitId) || {};
return { ...invoice, ...sector };
});
console.log(result);
答案 1 :(得分:0)
我建议您只是遍历发票,并使用具有唯一条目的集合中的条目充实条目。
这样的事情(没有测试代码,但我希望你能理解这个想法)
const data = invoices
.map(entry => {...entry, ...{
users.find(user => user.name === entry.name)
.map(user => {...user,
sector.find(sec=> sec.comitID === user.commitID)
})
}}
)
当您首先从集合中创建地图,然后仅查找联接属性而不是搜索它们时,您可以提高速度
const userMap = users.reduce((map, user) => {...map, ...{user.name: user}}, {})
const sectorMap = sector.reduce((map, sec) => {...map, ...{sector.comitID: sec}}), {})
const data = invoices.map(invoice => {...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID]})
答案 2 :(得分:0)
您可以将用户和部门项目移到地图中,并在必要时使用此对象。
const
users = [{ name: 'user1', comitId: 'aa1' }, { name: 'user2', comitId: 'aa2' }],
sector = [{ comitID: 'aa1', department: 'finance' }, { comitID: 'aa2', department: 'marketing' }, { comitID: 'aa3', department: 'engineering' }],
invoices = [{ name: 'user1', statementDate: '2/1/2019'}, { name: 'user1', statementDate: '2/14/2019' }, { name: 'user2', statementDate: '2/1/2019' }],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(({ name, statementDate }) => {
var { comitId } = userMap.get(name),
{ department } = sectorMap.get(comitId);
return { name, comitId, department, statementDate };
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 3 :(得分:0)
这是一个可行的基本脚本。
const users = [ // Unique Entries
{name:'user1', comitId: 'aa1'},
{name:'user2', comitId: 'aa2'}
]
const sectors = [ // Unique Entries
{comitID: 'aa1', department: 'finance'},
{comitID: 'aa2', department: 'marketing'},
{comitID: 'aa3', department: 'engineering'}
]
const invoices = [ // Multiple Entries
{name: 'user1', statementDate: '2/1/2019'},
{name: 'user1', statementDate: '2/14/2019'},
{name: 'user2', statementDate: '2/1/2019'}
]
sectors.forEach(sector => {
const user = users.find(user => sector.comitID === user.comitId);
if (user) {
user.department = sector.department;
}
});
const results = invoices.map(invoice => {
const user = users.find(user => invoice.name === user.name);
return Object.assign({}, user, { statementDate: invoice.statementDate });
});
console.log(results);
答案 4 :(得分:0)
您可以使用map
和filter
const users = [ // Unique Entries
{
name: 'user1',
comitId: 'aa1'
},
{
name: 'user2',
comitId: 'aa2'
}
]
const sector = [ // Unique Entries
{
comitID: 'aa1',
department: 'finance'
},
{
comitID: 'aa2',
department: 'marketing'
},
{
comitID: 'aa3',
department: 'engineering'
}
]
const invoices = [ // Multiple Entries
{
name: 'user1',
statementDate: '2/1/2019'
},
{
name: 'user1',
statementDate: '2/14/2019'
},
{
name: 'user2',
statementDate: '2/1/2019'
}
]
let newArray = invoices.map(function(item) {
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return {
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
}
});
console.log(newArray)