How to find items that are in one dstore collectio

2019-08-31 00:58发布

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?

标签: dojo dstore
1条回答
何必那么认真
2楼-- · 2019-08-31 01:53

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>
查看更多
登录 后发表回答