Arrays 如何使用RxJs将两个阵列合并为一个阵列,其中第二个阵列';每个元素将分配给每个对象属性的第一个数组?
我有两个界面,他们是团队和公司Arrays 如何使用RxJs将两个阵列合并为一个阵列,其中第二个阵列';每个元素将分配给每个对象属性的第一个数组?,arrays,angular,reactive-programming,rxjs6,rxjs-observables,Arrays,Angular,Reactive Programming,Rxjs6,Rxjs Observables,我有两个界面,他们是团队和公司 public interface Team { id: number; name: string; companyId: number; } public interface Company { id: number; name: string; } 以下是示例数据: "companies": [ { "id": 3, "name": "XSoftware", "location": "Nagar
public interface Team {
id: number;
name: string;
companyId: number;
}
public interface Company {
id: number;
name: string;
}
以下是示例数据:
"companies": [
{
"id": 3,
"name": "XSoftware",
"location": "Nagar"
},
{
"id": 5,
"name": "Google",
"location": "Seattle"
},
{
"id": 7,
"name": "YS",
"location": "Dhanmondi"
},
{
"id": 8,
"name": "Amazon",
"location": "Seattle DC"
},
{
"name": "ToTD",
"location": "Pink City",
"id": 10
}
]
因此,我想根据公司ID将公司作为属性分配给每个团队。
像这样:
"teams": [
{
"id": 1,
"name": "Team X",
"expertise": "Java",
"companyId": 3,
"company": {
"id": 3,
"name": "XSoftware",
"location": "Nagar"
}
},
{
"id": 2,
"name": "Team Y",
"expertise": "Angular",
"companyId": 3,
"company": {
"id": 3,
"name": "XSoftware",
"location": "Nagar"
}
},
{
"id": 3,
"name": "Team Z",
"expertise": "Spring Boot",
"companyId": 8,
"company": {
"id": 8,
"name": "Amazon",
"location": "Seattle DC"
}
},
{
"id": 4,
"name": "Team M",
"expertise": "Node Js",
"companyId": 5,
"company": {
"id": 5,
"name": "Google",
"location": "Seattle"
}
}
]
那么如何使用RxJs实现这一点呢。
我有两个观测值,分别返回团队[]和公司[]的观测值
const teams$: Observable<Team[]> = this.httpClient.get<Team[]>('/teams');
const companies$: Observable<Company[]> = this.httpClient.get<Company[]>('/companies');
constteams$:Observable=this.httpClient.get('/teams');
const companys$:Observable=this.httpClient.get('/companys');
那么,如何做到这一点呢?我知道可以用命令式的方式(通过使用循环、if-else等)来完成,但我想用反应式的方式来完成,只使用反应式的操作符、观察者 如前所述,一种方法是使用forkJoin
操作符。请看下面的代码片段
const teams$ = this.httpClient.get<Team[]>('/teams');
const companies$ = this.httpClient.get<Company[]>('/companies');
const teamsWithCompanies = forkJoin(teams$, companies$).pipe(
switchMap((values: any[]) => {
const teamsWithCompaniesResponse: TeamWithCompany[] = [];
const teamArray = values[0];
teamArray.forEach((team) => {
const teamWithCompany: TeamWithCompany = {
id: team.id,
name: team.name,
expertise: team.expertise,
companyId: team.companyId,
company: values[1].find(c => c.id === team.companyId)
}
teamsWithCompaniesResponse.push(teamWithCompany);
})
return of(teamsWithCompaniesResponse);
})
).subscribe(response => {
console.log('[RESPONSE]', response);
})
constteams$=this.httpClient.get('/teams');
const companys$=this.httpClient.get('/companys');
const teamsWithCompanies=forkJoin(teams$,companies$).pipe(
switchMap((值:any[])=>{
const TeamSwithCompanys响应:TeamWithCompany[]=[];
const teamArray=值[0];
teamArray.forEach((团队)=>{
const teamWithCompany:teamWithCompany={
id:team.id,
name:team.name,
专业知识:团队,专业知识,
公司ID:team.companyId,
公司:值[1]。查找(c=>c.id==team.companyId)
}
TeamWithCompanyResponse.push(teamWithCompany);
})
返回(TeamSwithCompanyResponse);
})
).订阅(响应=>{
log(“[RESPONSE]”,RESPONSE);
})
如前所述,一种方法是使用forkJoin
操作符。请看下面的代码片段
const teams$ = this.httpClient.get<Team[]>('/teams');
const companies$ = this.httpClient.get<Company[]>('/companies');
const teamsWithCompanies = forkJoin(teams$, companies$).pipe(
switchMap((values: any[]) => {
const teamsWithCompaniesResponse: TeamWithCompany[] = [];
const teamArray = values[0];
teamArray.forEach((team) => {
const teamWithCompany: TeamWithCompany = {
id: team.id,
name: team.name,
expertise: team.expertise,
companyId: team.companyId,
company: values[1].find(c => c.id === team.companyId)
}
teamsWithCompaniesResponse.push(teamWithCompany);
})
return of(teamsWithCompaniesResponse);
})
).subscribe(response => {
console.log('[RESPONSE]', response);
})
constteams$=this.httpClient.get('/teams');
const companys$=this.httpClient.get('/companys');
const teamsWithCompanies=forkJoin(teams$,companies$).pipe(
switchMap((值:any[])=>{
const TeamSwithCompanys响应:TeamWithCompany[]=[];
const teamArray=值[0];
teamArray.forEach((团队)=>{
const teamWithCompany:teamWithCompany={
id:team.id,
name:team.name,
专业知识:团队,专业知识,
公司ID:team.companyId,
公司:值[1]。查找(c=>c.id==team.companyId)
}
TeamWithCompanyResponse.push(teamWithCompany);
})
返回(TeamSwithCompanyResponse);
})
).订阅(响应=>{
log(“[RESPONSE]”,RESPONSE);
})
首先,您需要团队和公司对象来合并它们。所以你能做的就是
this.httpClient.get<Team[]>('/teams').subscribe(teams => {
this.httpClient.get<Company[]>('/companies').subscribe(companies => {
//here you have teams and companies array
var result = [];
teams.forEach(team => {
var company = companies.find(s => s.id == team.companyId);
if(company){
Object.assign(team , {company});
result.push(team);
}
})
console.log(result);
})
})
this.httpClient.get('/teams').subscribe(teams=>{
this.httpClient.get('/companys').subscribe(companys=>{
//这里有团队和公司阵列
var结果=[];
teams.forEach(团队=>{
var company=companys.find(s=>s.id==team.companyId);
if(公司){
分配(团队,{company});
结果:推动(团队);
}
})
控制台日志(结果);
})
})
这应该能奏效
另外,如果你在不同的地方有这两个观测值,你会找到一种方法让这两个数组合并它
谢谢首先,您需要团队和公司对象来合并它们。所以你能做的就是
this.httpClient.get<Team[]>('/teams').subscribe(teams => {
this.httpClient.get<Company[]>('/companies').subscribe(companies => {
//here you have teams and companies array
var result = [];
teams.forEach(team => {
var company = companies.find(s => s.id == team.companyId);
if(company){
Object.assign(team , {company});
result.push(team);
}
})
console.log(result);
})
})
this.httpClient.get('/teams').subscribe(teams=>{
this.httpClient.get('/companys').subscribe(companys=>{
//这里有团队和公司阵列
var结果=[];
teams.forEach(团队=>{
var company=companys.find(s=>s.id==team.companyId);
if(公司){
分配(团队,{company});
结果:推动(团队);
}
})
控制台日志(结果);
})
})
这应该能奏效
另外,如果你在不同的地方有这两个观测值,你会找到一种方法让这两个数组合并它
收到两个阵列响应后,谢谢您
let arr3 = this.teams.map((item, i) => Object.assign({}, item, { company: (this.companies.find((itmInner) => itmInner.id === this.teams[i].companyId)) }));
console.log(arr3);
在获得两个数组响应之后
let arr3 = this.teams.map((item, i) => Object.assign({}, item, { company: (this.companies.find((itmInner) => itmInner.id === this.teams[i].companyId)) }));
console.log(arr3);
我认为“以rxjs方式”这样做没有任何意义,因为您得到的响应是每个请求一个发出的数组,而不是流。我只需要对两个可观察对象执行一个
forkJoin
,然后执行所有必要的操作(或者我会以一种更实用的方式执行),我认为“以rxjs方式”这样做没有任何意义,因为您得到的响应是每个请求一个发出的数组,而不是流。我只需要对两个观察对象执行一个forkJoin
,然后执行所有必要的操作(或者我会以更有效的方式执行),在订阅中执行订阅是一种不好的做法,性能会更低。使用您的代码,您需要先获得/teams
,然后才能获得/companys
,这可以并行完成。在订阅中进行订阅是一种不好的做法,而且会降低性能。使用您的代码,您需要先获得/teams
,然后才能获得/companys
,这可以并行完成。它可以工作,但我可以使用zip运算符吗?我只是想知道。ForkJoin为我做这项工作。谢谢,但我可以使用zip操作符吗?我只是想知道。ForkJoin为我做这项工作。谢谢