Arrays 基于可观测数组2的字段值将数据映射到可观测数组1

Arrays 基于可观测数组2的字段值将数据映射到可观测数组1,arrays,angular,rxjs,google-cloud-firestore,angularfire,Arrays,Angular,Rxjs,Google Cloud Firestore,Angularfire,我试图根据arr2中的字段值将数据映射到arr1,但遇到了数据请求顺序的问题。 我在package.json文件中使用下面列出的语言 为了(希望)更好地解释我试图做的事情:有两个数组(例如,arr1={Locations[]}和arr2={Activated Locations[]),其中基本数组是Firestore集合,嵌套文档是内部数组-这两个数组都适用于arr1和arr2。因此,内部数组包含一个字段“activatedId”这就是我试图使用的公共元素,用于确定arr1的内部数组是否包含映射

我试图根据arr2中的字段值将数据映射到arr1,但遇到了数据请求顺序的问题。 我在package.json文件中使用下面列出的语言

为了(希望)更好地解释我试图做的事情:有两个数组(例如,arr1={Locations[]}和arr2={Activated Locations[]),其中基本数组是Firestore集合,嵌套文档是内部数组-这两个数组都适用于arr1和arr2。因此,内部数组包含一个字段“activatedId”这就是我试图使用的公共元素,用于确定arr1的内部数组是否包含映射为“已激活”的字段:true(或false)。 另外,我相信很多人会嘲笑我的代码,因为我刚刚被大多数语言弄湿了:)所以请友好一点!谢谢你提供的任何帮助,如果你对如何更有效地完成这项工作有什么建议,我很想听听你的意见

谢谢你的支持

    export class LocationListingModel{
      id: string;
      title: string;
      activateId?: string;
      activated: boolean;
    }

下面是一大段代码,我认为它与您想要的非常接近:

/*
*用于检查所有内容的虚拟数据
*/
常数位置=[
{activateId:'1234',activated:false},
{activateId:'2345',activated:false},
{activateId:'3456',activated:false},
{activateId:'4567',activated:false},
{activateId:'5678',activated:false},
{activateId:'6789',activated:false},
];
常数激活位置=[
{activatedId:'1234'},
{activatedId:'3456'},
{activatedId:'6789'}
];
/*
*接下来,我们将activatedLocations数组映射到一个对象
*谁的键是activatedIds,谁的值为true
*/
const indexedActivatedLocations$=的(激活位置)。管道(
地图(locs=>{
常量索引={};
对于(让loc的loc){
索引[loc.activatedId]=真;
}
收益指数;
//索引:{'1234':true,'3456':true,'6789'true}
})
);
/*
*最后,我们使用forkJoin订阅可观察对象和
*将其结果作为数组发出。
*将每个location.activateId与新对象的关键帧进行比较
*比反复搜索数组快得多
*我们改变位置,必要时激活,然后
*发射更新的阵列。
*/
const updatedLocations$=forkJoin({
地点:共(个地点),
已激活:indexedActivatedLocations$
}).烟斗(
地图(数据=>{
用于(数据位置的位置){
if(数据激活[loc.activatedId]){
loc.activated=true;
}
}
返回数据和位置;
})
);

恐怕你的问题还不清楚。你的代码中有太多的活动部分,其中许多根本不起作用,你的最终目标也不明确。你说两个数组之间的“公共域”是什么意思?如
arr1[0]
vs
arr2[0]
?这两个数组是由可观察对象发出的吗?部分。有两个数组(例如arr1={Locations[]}和arr2={Activated Locations[]),其中基本数组是Firestore集合,嵌套文档是内部数组-适用于arr1和arr2。因此,内部数组包含一个字段“activatedId”这就是我试图使用的公共元素,用于确定arr1的内部数组是否包含映射为“已激活”的字段:true(或false)。希望这能澄清问题。这对我来说是一个头疼的问题,我不太擅长向真实的人解释。哈哈。因此,如果我没有弄错,那么对于
LocationListingModel[]
数组中的每个元素,您需要检查
ActiveLocationsModel[]中是否有任何元素
activatedId
值匹配的数组,如果匹配,请将
LocationListingModel
已激活的
值设置为
true
?我同意Will的观点-很难确定您试图实现的目标。但是,如果您希望将这两个数组都提供给Observa中的每个操作员ble chain,那么可能会有帮助。@WillAlexander完全正确。它看起来非常接近!我看到了我希望在虚拟数据上得到的结果,但一旦我添加到afs集合中,for(let loc of locs)就会在for(let of)和for上抛出错误“Type'Observable'必须有一个返回迭代器的“[Symbol.iterator]”方法如果你仔细想想,这是有道理的。我创建了一个发射阵列的可观测对象,你所需要做的就是用发射阵列的可观测对象替换它。你是一个圣人,先生。我欠你的债。别忘了接受答案来帮助其他有同样问题的人:)
    export class ActiveLocationsModel {
      id: string;
      title: string;
      activateId?: string;
    }
private  activatedCheck(locationId: string, activatedId: string): boolean {
const locRef = this.afs.doc('locations/' + locationId);
const check = this.afs.collection<LocationDealListingModel>('activatedLocations/', ref => ref
.where('locRef', '==', locRef.ref)
.where('redeemId', '==', activatedId)).snapshotChanges();
if (check.subscribe(res => res.length > 0 )){
  return true;

} else {
  return false;
};
}



 public getsActivatedLocationDataSource(locationId: string, userId: string): Observable<Array<LocationListingModel>> {
let activated: boolean;
const locRef = this.afs.doc('locations/' + locationId);


return this.afs.collection<AvailableDealModel>('locations/', ref => ref.where('locRef', '==', locRef.ref))
  .valueChanges({ idField: 'id' })
  .pipe(map(actions => actions
    .map(data => {
      const locRef = this.afs.doc('userProfile/' + userId);
      const checkExists = this.afs.collection<LocationDealListingModel>('activeLocations/', ref => ref
        .where('locRef', '==', locRef.ref)
        .where('activeId', '==', data.activeId)).snapshotChanges();
      if (checkExists && (checkExists.subscribe(res => res.length > 0))) {
        activated = true;
      } else if (checkExists && (checkExists.subscribe(res => res.length === 0))) {
        activated = false;
      } else {
        console.log('error completing check. ')
      }

      const id = data.id;
      return { id, active: activated, ...data } as LocationListingModel;

    })
  ));
 public getsActivatedLocationDataSource(locationId: string, userId: string): Observable<Array<LocationListingModel>> {
let activated: boolean;
const locRef = this.afs.doc('locations/' + locationId);


return this.afs.collection<AvailableDealModel>('locations/', ref => ref.where('locRef', '==', locRef.ref))
  .valueChanges({ idField: 'id' })
        .pipe(map(actions => actions
    .map(data => {
      const checkExists = this.activatedCheck(locationId, activatedId);
      if (checkExists === true) {
        activated = true;
      } else if (checkExists === false) {
        activated = false;
      } else {
        console.log('error completing check. ')
      }
      const id = data.id;
      return { id, active: activated, ...data } as LocationListingModel;
    })
  ));
  "dependencies": {
    "@agm/core": "^1.0.0-beta.6",
    "@agm/snazzy-info-window": "^1.0.0-beta.6",
    "@angular/animations": "8.2.1",
    "@angular/cdk": "~8.1.3",
    "@angular/common": "8.2.1",
    "@angular/core": "8.2.1",
    "@angular/fire": "^5.2.1",
    "@angular/forms": "8.2.1",
    "@angular/material": "^8.1.3",
    "@angular/platform-browser": "8.2.1",
    "@angular/platform-browser-dynamic": "8.2.1",
    "@angular/pwa": "~0.802.1",
    "@angular/router": "8.2.1",
    "@angular/service-worker": "8.2.1",
    "@capacitor/android": "^1.0.0",
    "@capacitor/cli": "^1.0.0",
    "@capacitor/core": "^1.0.0",
    "@capacitor/ios": "^1.0.0",
    "@ionic-native/browser-tab": "^5.8.0",
    "@ionic-native/call-number": "^5.8.0",
    "@ionic-native/core": "^5.8.0",
    "@ionic-native/date-picker": "^5.8.0",
    "@ionic-native/facebook": "^5.8.0",
    "@ionic-native/geolocation": "^5.12.0",
    "@ionic-native/google-plus": "^5.12.0",
    "@ionic-native/ionic-webview": "^5.8.0",
    "@ionic-native/keyboard": "^5.8.0",
    "@ionic-native/native-storage": "^5.12.0",
    "@ionic-native/network": "^5.8.0",
    "@ionic-native/splash-screen": "^5.8.0",
    "@ionic-native/status-bar": "^5.8.0",
    "@ionic/angular": "^4.7.4",
    "@ionic/storage": "^2.2.0",
    "@logisticinfotech/ionic4-datepicker": "^1.4.1",
    "@ngrx/store": "^8.2.0",
    "@ngx-translate/core": "^11.0.1",
    "@ngx-translate/http-loader": "^4.0.0",
    "@types/jest": "^24.0.17",
    "angular-pipes": "^9.0.2",
    "angular-star-rating": "^4.0.0-beta.3",
    "angularjs-datepicker": "^2.1.23",
    "cordova-plugin-browsertab": "^0.2.0",
    "cordova-plugin-call-number": "^1.0.1",
    "cordova-plugin-datepicker": "^0.9.3",
    "cordova-plugin-facebook4": "^4.2.1",
    "cordova-plugin-geolocation": "^4.0.2",
    "cordova-plugin-googleplus": "^7.0.2",
    "cordova-plugin-keyboard": "^1.2.0",
    "cordova-plugin-nativestorage": "^2.3.2",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-sqlite-storage": "^3.2.1",
    "core-js": "^2.5.7",
    "dayjs": "1.8.0",
    "firebase": "^6.4.0",
    "geofire": "^5.0.1",
    "geofirex": "0.0.6",
    "google-libphonenumber": "^3.2.1",
    "hammerjs": "^2.0.8",
    "moment": "^2.24.0",
    "ng2-file-upload": "^1.3.0",
    "ngx-paypal": "^5.0.0",
    "promise-polyfill": "^8.1.3",
    "rxfire": "^3.6.8",
    "rxjs": "6.5.2",
    "rxjs-compat": "^6.5.2",
    "snazzy-info-window": "^1.1.1",
    "tslib": "^1.9.0",
    "videogular2": "^7.0.1",
    "zone.js": "^0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "^0.802.1",
    "@angular-devkit/build-angular": "~0.802.1",
    "@angular-devkit/core": "^8.2.1",
    "@angular-devkit/schematics": "^8.2.1",
    "@angular/cli": "^8.2.1",
    "@angular/compiler": "8.2.1",
    "@angular/compiler-cli": "8.2.1",
    "@angular/language-service": "8.2.1",
    "@commitlint/cli": "^8.1.0",
    "@commitlint/config-angular": "^8.1.0",
    "@ionic/angular-toolkit": "^2.0.0",
    "@ionic/lab": "2.0.7",
    "@types/core-js": "^2.5.0",
    "@types/googlemaps": "^3.36.2",
    "@types/node": "12.0.0",
    "@webcomponents/webcomponentsjs": "^2.2.10",
    "codelyzer": "^5.0.1",
    "husky": "^1.3.1",
    "ts-node": "~8.1.0",
    "tslint": "~5.16.0",
    "typescript": "~3.5.3"
  }