我有三个数组,其中的数据如下所示。
client = [{id: 1, name: client 1}, {id: 2, name: client 2}];
company = [{id: 1, name: company 1}, {id: 2, name: company 2}];
case = [{id: 1, client: 1, company: 2, name: case 1},
{id: 2, client: 1, company: 1, name: case 2}];
在我看来,我需要将其显示为
我该如何实现?我的基本要求是将case数组ID替换为相关数组的名称。我是angular的新手,使用的是angular4。在此先感谢。
答案 0 :(得分:1)
使用forech和find一起创建一个新数组。
clients = [{id: 1, name: 'client 1'}, {id: 2, name: 'client 2'}];
companies = [{id: 1, name: 'company 1'}, {id: 2, name: 'company 2'}];
case: any[] = [{id: 1, client: 1, company: 2, name: 'case 1'},
{id: 2, client: 1, company: 1, name:'case 2'}];
custom = [];
constructor(){
this.case.forEach((item) => {
let company = this.companies.find((c) => c.id === item.company)
let client = this.clients.find((c) => c.id === item.client)
this.custom.push({
id : item.id,
company: (company) ? company.name : null,
client: (client) ? client.name : null,
name: item.name
})
})
}
答案 1 :(得分:1)
由于您的联接表为[Route("getprofile/{id:guid}")]
public async Task<Profile> GetProfile(string id)
{
...
}
,因此可以对其进行迭代并使用reduce
在表中创建一个新数组:
case
编辑为此,您甚至可以通过扩展来减少代码:
const clients = [{id: 1, name: 'client 1'}, {id: 2, name: 'client 2'}];
const companies = [{id: 1, name: 'company 1'}, {id: 2, name: 'company 2'}];
const cases = [{id: 1, client: 1, company: 2, name: 'case 1'}, {id: 2, client: 1, company: 1, name: 'case 2'}];
const joined = cases.reduce((p ,n) => {
p.push({
client: clients.find(c => c.id === n.client).name,
company: companies.find(c => c.id === n.company).name,
case: n.name
});
return p;
}, []);
console.log(joined);