softshipper
softshipper

Reputation: 34087

RxJS filter date

I have two arrays, for example:

var a1 = ["1","3","4","5"]
var a2 = ["1","6","3","5"]

the second array is used as a storage.

I want to compare this two arrays, so for example, the number 4 is in a1 but not in a2, so I want push the number 4 into a2.
So every number does not contain already in a2 should be pushed into it.

How can I solve this scenario with rxjs?

Upvotes: 0

Views: 273

Answers (1)

martin
martin

Reputation: 96939

The easiest way I can think of would be the following:

var a1 = ["1","3","4","5"];
var a2 = ["1","6","3","5"];

Observable.from(a1)
    .filter(val => a2.indexOf(val) === -1)
    .subscribe(val => {
        a2.push(val);
    });

console.log(a2);

This prints to console:

[ '1', '6', '3', '5', '4' ]

See live demo: https://jsbin.com/gabesog/1/edit

Upvotes: 2

Related Questions