基于'contain'的数组分区
Array partition based on 'contain'
var array = [ "123-foo", "456-bar", "789-baz" ];
var selected = [ "123", "789" ];
var partition_contains = _.partition(array, function(value){
return _.contains(selected, value);
});
console.log(partition_contains);
var partition_filter = _.partition(array, function(value){
return !!_.filter(selected, function(s){ return value.indexOf(s) !== -1; }).length;
});
console.log(partition_filter);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
输出为:
[
[],
[
"123-foo",
"456-bar",
"789-baz"
]
]
预期输出:
[
[
"123-foo",
"789-baz"
],
[
"456-bar"
]
]
尽管内部使用了 indexOf
,但为什么没有像我预期的那样包含工作?
编辑:适用于 filter
但想知道为什么不包含?
从第一种方法来看,我认为这是因为 _.contains
匹配数组和目标值之间的精确值。
console.log(_.contains(selected, "123-foo")); // false
我的解决方案将使用 Array 中的 some
方法。
var partitioned = _.partition(array, value => selected.some(select => value.indexOf(select) > -1));
console.log(partitioned);
我看到underscore.js也有_.some
方法。
下划线的 .contains
:
https://underscorejs.org/#contains
_.contains(list, value, [fromIndex])
Aliases: include, includes
Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.
您 array
中的 None 个项目在 selected
中,因此测试总是失败。 indexOf
只有 returns 0
+ 个数组项 完全匹配 正在搜索的项。
试试这个,检查 selected
项中的 some
项是否包含在 value
中:
var array = [ "123-foo", "456-bar", "789-baz" ];
var selected = [ "123", "789" ];
var partition_filter = _.partition(array, value => (
selected.some(sel => value.includes(sel))
));
console.log(partition_filter);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
var array = [ "123-foo", "456-bar", "789-baz" ];
var selected = [ "123", "789" ];
var partition_contains = _.partition(array, function(value){
return _.contains(selected, value);
});
console.log(partition_contains);
var partition_filter = _.partition(array, function(value){
return !!_.filter(selected, function(s){ return value.indexOf(s) !== -1; }).length;
});
console.log(partition_filter);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
输出为:
[
[],
[
"123-foo",
"456-bar",
"789-baz"
]
]
预期输出:
[
[
"123-foo",
"789-baz"
],
[
"456-bar"
]
]
尽管内部使用了 indexOf
,但为什么没有像我预期的那样包含工作?
编辑:适用于 filter
但想知道为什么不包含?
从第一种方法来看,我认为这是因为 _.contains
匹配数组和目标值之间的精确值。
console.log(_.contains(selected, "123-foo")); // false
我的解决方案将使用 Array 中的 some
方法。
var partitioned = _.partition(array, value => selected.some(select => value.indexOf(select) > -1));
console.log(partitioned);
我看到underscore.js也有_.some
方法。
下划线的 .contains
:
https://underscorejs.org/#contains
您
_.contains(list, value, [fromIndex])
Aliases: include, includesReturns true if the value is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.
array
中的 None 个项目在 selected
中,因此测试总是失败。 indexOf
只有 returns 0
+ 个数组项 完全匹配 正在搜索的项。
试试这个,检查 selected
项中的 some
项是否包含在 value
中:
var array = [ "123-foo", "456-bar", "789-baz" ];
var selected = [ "123", "789" ];
var partition_filter = _.partition(array, value => (
selected.some(sel => value.includes(sel))
));
console.log(partition_filter);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>