How to check if key: value pair is in Observable

Hey I add values to an Observable like this:

    favoriteSounds.add({ id: soundID});

But how do I check if a certain soundID is inside the Overservable? I tried

    favoriteSounds.contains({ id: soundID})

and

    favoriteSounds.contains(soundID)

But both say the soundID is not inside, although it is :frowning:

The documentation only covers the simple case:

Hi!

Observables do not have any knowledge of key-value pairs. An observable only contains values, objects - in your case.

To achieve what you want, try this:

favoriteSounds.count(function(x) { return x.id === soundID; }).count().map(function(x){ return x > 0; })

@Anders perhaps a findOne/find method, like in MongoDB could be useful:

var list = Observable({x:1}, {x:2});

I’m always for promise approach over others, but it could be just as simple as:

var obj = {x:1};
var list = Observable(obj, {x:2});

var X1 = list.findOne({x:1});

X1.value === obj; // true
// or
X1 === obj; // true, otherwise X1 is null

I kindof like the way the contains method is already, but perhaps it could return an Observable instead so that its subscribed to the list Observable:

var X1 = {x:1};
var list = Observable(obj, {x:2});

var containsX1 = list.contains(obj);

if(containsX1.value) {
    // do stuff if list contains obj
}

btw Anders I’m still having trouble understnding this from the docs:

var items = conditionObservable.map(function(v) {
    return itemsObservable.where(function(x) { return v != x; });
}).inner();

This lets you create an Observable which pushes changes whenever the condition or the data changes.

I was trying to use that somehow for making a Boolean Observable that’s tied to list with the current implementation of contains.

Also I saw you talk about it in some other forum post, but I’m still confused on it.

Thank you Anders, it’s working :slight_smile: