假设我有两个 dstore 集合:value1 和 value2。
我想找出 value1 中有哪些项目,但 value2 中没有。所以是这样的:
var filterCollection = value1.filter(function(item) {
return value2.notExists(item);
});
但是“notExists”函数并不存在。我如何使这个逻辑起作用?
由于默认情况下不包含此功能dstore
,您可以编写自己的函数来比较您的内容dstores
并根据需要使用结果。
这是一个通用的例子。您需要value1
使用.value1
dstore
简化示例。
http://jsbin.com/fozeqamide/edit?html,控制台,输出
不使用 indexOf() 的版本
http://jsbin.com/nihelejodo/edit?html,控制台,输出
循环的方式datastore
与它的数据结构有关。
注意:这是一个通用示例,因为该问题不提供任何源代码,也不提供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>