How to find items that are in one dstore collection but not in another?

229 views Asked by At

Suppose I have two dstore collections: value1 and value2.

I want to find out what items are in value1 but not in value2. So something like this:

var filterCollection = value1.filter(function(item) {
    return value2.notExists(item);
});

But "notExists" function, well, doesn't exist. How do I make this logic work?

1

There are 1 answers

4
GibboK On BEST ANSWER

As this feature is not included by default in dstore, you can write your own function for comparing the content of your dstores and use the result as you wish.

Here a generic example. You need to populate value1 and value1 with the content of your dstore.

Simplified example.

http://jsbin.com/fozeqamide/edit?html,console,output

Version without using indexOf()

http://jsbin.com/nihelejodo/edit?html,console,output

The way how you loop in your datastore is related to its data structure.

Notes: This is a generic example as the question does not provide any source code, nor an example of data in dstore.

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Builder</title>
    <style>
    </style>
    <script>
        window.app = {
            value1: [1, 2, 3, 4, 5, 6, 4, 8], // populate with date for valu1 dstore
            value2: [1, 2, 6, 4, 8], // populate with date for valu1 dstore
            valueNotExists: [],
            start: function () {
                this.notExists();
            },
            notExists: function () {
                this.valueNotExists = this.value1.filter(function (x) {
                    return this.value2.indexOf(x) < 0;
                }.bind(this));
                console.log(this.valueNotExists);
            },
        };
    </script>

</head>

<body onload="window.app.start();">

</body>
</html>