根据可观察阵列2中的字段值将数据映射到可观察阵列1

时间:2019-08-24 17:31:36

标签: arrays angular rxjs google-cloud-firestore angularfire

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

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

感谢Noob!

_calculateImageDimension().then((size) => print("size = ${size}")); // 487.0,696.0
ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4

ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4
    export class LocationListingModel{
      id: string;
      title: string;
      activateId?: string;
      activated: boolean;
    }

}

或使用第一个功能-ActivatedCheck()

    export class ActiveLocationsModel {
      id: string;
      title: string;
      activateId?: string;
    }

package.json

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;

    })
  ));

1 个答案:

答案 0 :(得分:0)

所以这是一大堆我认为与您想要的代码很接近的代码:

/*
* Dummy data to check everything with
*/
const locations = [
  { activatedId: '1234', activated: false },
  { activatedId: '2345', activated: false },
  { activatedId: '3456', activated: false },
  { activatedId: '4567', activated: false },
  { activatedId: '5678', activated: false },
  { activatedId: '6789', activated: false },
];
const activatedLocations = [
  { activatedId: '1234' },
  { activatedId: '3456' },
  { activatedId: '6789' }
];

/*
* Next we map the activatedLocations Array to an Object
* whose keys are the activatedIds and whose values are true
*/
const indexedActivatedLocations$ = of(activatedLocations).pipe(
  map(locs => {
    const index = {};
    for (let loc of locs) {
      index[loc.activatedId] = true;
    }
    return index;
    // index: { '1234': true, '3456': true, '6789' true }
  })
);

/*
* Finally, we use forkJoin to subscribe to both Observables and
* emit their results as an Array.
* Comparing each location.activatedId to the new Object's keys
* is much faster than searching through an Array again and again
* We change location.activated where necessary and then
* emit the updated Array.
*/
const updatedLocations$ = forkJoin({
  locations: of(locations),
  activated: indexedActivatedLocations$
}).pipe(
  map(data => {
    for (let loc of data.locations) {
      if (data.activated[loc.activatedId]) {
        loc.activated = true;
      }
    }
    return data.locations;
  })
);