Ric*_*ard 3 javascript underscore.js
使用Underscore,我可以检查列表是否包含单个项目:
_.contains([1, 2, 3], 3);
Run Code Online (Sandbox Code Playgroud)
现在我想扩展它以检查列表是否包含另一个列表中的任何项目:
_.contains([1, 2, 3], [4, 1]);
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的,但是有更简洁的方法吗?
var isInList = _.some(_.map([1, 2, 3], function(d){ return _.contains([4, 1], d); }));
Run Code Online (Sandbox Code Playgroud)
使用_.intersection函数,像这样
var isInList = _.intersection([1, 2, 3], [4, 1]).length !== 0;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
445 次 |
| 最近记录: |