如何通过 underscorejs 过滤 table
how to filter table by underscorejs
我用下划线函数工作,我想按作者 table 中不存在的元素过滤 table
我想最终显示这个 [[10,20,30,50,60],[80,66],[9,70,4,3] ]
var tab=[];
_.each([[10,20,30,5,50,60],[80,6,66,7,8,2],[9,70,4,3,1]], function (c) {
var k= _.filter(c, function (cel) {
return _.some([1, 2, 5, 6, 8, 7], function (el) {
return cel != el
})
})
tab.push(k);
});
console.log(tab)
使用 _.every
而不是 _.some
因为 some
函数将 return 如果过滤器数组中的任何元素与 table。因此,在每种情况下,您都将数组的元素与数字 1(过滤器数组的第一个元素)进行比较,并且由于 table != 1 中的大多数数字,_.some
函数是 returning true
所以数字被添加到 _.filter
函数的结果中。
基本上,你可以使用
_.map
用于获取带过滤数组的数组,
_.filter
只获取与给定值不匹配的值和
_.contains
用于检查带有值数组的项目。
var array = [[10, 20, 30, 5, 50, 60], [80, 6, 66, 7, 8, 2], [9, 70, 4, 3, 1]],
values = [1, 2, 5, 6, 8, 7],
result = _.map(array, function (a) {
return _.filter(a, function (v) {
return !_.contains(values, v);
});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
我用下划线函数工作,我想按作者 table 中不存在的元素过滤 table 我想最终显示这个 [[10,20,30,50,60],[80,66],[9,70,4,3] ]
var tab=[];
_.each([[10,20,30,5,50,60],[80,6,66,7,8,2],[9,70,4,3,1]], function (c) {
var k= _.filter(c, function (cel) {
return _.some([1, 2, 5, 6, 8, 7], function (el) {
return cel != el
})
})
tab.push(k);
});
console.log(tab)
使用 _.every
而不是 _.some
因为 some
函数将 return 如果过滤器数组中的任何元素与 table。因此,在每种情况下,您都将数组的元素与数字 1(过滤器数组的第一个元素)进行比较,并且由于 table != 1 中的大多数数字,_.some
函数是 returning true
所以数字被添加到 _.filter
函数的结果中。
基本上,你可以使用
_.map
用于获取带过滤数组的数组,_.filter
只获取与给定值不匹配的值和_.contains
用于检查带有值数组的项目。
var array = [[10, 20, 30, 5, 50, 60], [80, 6, 66, 7, 8, 2], [9, 70, 4, 3, 1]],
values = [1, 2, 5, 6, 8, 7],
result = _.map(array, function (a) {
return _.filter(a, function (v) {
return !_.contains(values, v);
});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>