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

北城余情 提交于 2020-01-06 13:53:49

问题


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:


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>


来源:https://stackoverflow.com/questions/31019023/how-to-find-items-that-are-in-one-dstore-collection-but-not-in-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!