-4

Hi All,

I'm pretty new to angular and rxjs. I have two beservable arrays which one of them 'A' has only subset of the other observable array 'B'. I need to remove elements from 'A' from 'B'. How do I achieve that?

  • Here you can find some information about what should be asked on stackoverflow https://stackoverflow.com/help/on-topic . Your question does not contain any details about what you've tried and where you got stuck. – ikos23 Mar 27 '18 at 04:09

4 Answers4

0

You can do it by writing JS that will iterate arrays but would be easy for you if you use lodash's difference method. Here you can find it https://lodash.com/docs/4.17.5#difference

Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46
0

Have you retrieved all elements from the arrays A and B and then removed the subscription? In this case, they are simple arrays.. How can I subtract one array from another? will help.. However, if the subscription is live and running, then you in the subscription handler for subset, you need to manually check if the item got already exists in 2nd array, and then remove that from 2nd array. Since you have assumed it to be a subset, then only removal operation will be required. If you wanna join with duplicate removal, you need to add to the 2nd array if not present in there. Make sure you have an identity value to compare an object against another.

NitinSingh
  • 2,029
  • 1
  • 15
  • 33
0
import { map } from'rxjs/operators/map';
import { Observable } from 'rxjs/Observable'
import { of } from 'rxjs/observable/of';
import { forkJoin } from 'rxjs/observable/forkJoin';   

const arr1$ = of([1,2,3,4,5])
const arr2$ = of([1,2,3]);

const result$ = forkJoin(arr1$, arr2$).pipe(map(([arr1, arr2]) => arr1.filter(x => arr2.indexOf(x) === -1)));

result$.subscribe(console.log)
Tomasz Kula
  • 16,199
  • 2
  • 68
  • 79
-1

Normally, in Angular2+, you should unsubscribe all observers in OnDestroy, it maybe looks like this:

ngOnDestroy() {
      this.subscribers.forEach(item => item.unsubscribe())
}

subscribers is an array, you should replace it with A or B.

Nothing Mi
  • 232
  • 1
  • 8