如何查找在一个dstore集合中但不在另一个dstore集合中的项目? [英] How to find items that are in one dstore collection but not in another?

查看:62
本文介绍了如何查找在一个dstore集合中但不在另一个dstore集合中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个dstore集合:value1和value2。

Suppose I have two dstore collections: value1 and value2.

我想找出value1而不是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);
});

但是 notExists功能不是存在。我如何使这种逻辑起作用?

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

推荐答案

由于默认情况下 dstore ,您可以编写自己的函数来比较 dstores 的内容,并根据需要使用结果。

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.

这里是一个通用示例。您需要使用 dstore value1 value1 c>。

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

简化示例。

http://jsbin.com/fozeqamide/edit?html,控制台,输出

不使用indexOf()的版本

Version without using indexOf()

http://jsbin.com/nihelejodo/edit?html,控制台,输出

中循环的方式数据存储与它的数据结构有关。

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

注意:这是一个通用示例,因为该问题未提供任何源代码,也不提供示例 dstore 中的数据。

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>

这篇关于如何查找在一个dstore集合中但不在另一个dstore集合中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆