使用lodash检查其他元素集中是否存在元素集

时间:2016-02-21 10:56:16

标签: javascript node.js coffeescript lodash

字段game_options设置了一些参数 我需要查看请求的选项是否与game_options

相同
game_options = ['ps2', 'pc']
request = ['ps2', 'pc', 'xbox']

此组合应返回false 但是这个:是的:

game_options = ['ps2', 'pc', 'xbox']
request = ['ps2', 'pc']

我用lodash完成了这个,但不确定它是否是正确的方法:

_ = require 'lodash'
hasAllRequestedOptions - (options, request) ->
  return true if _.intersection(game_options, request).length is request.length
  return false

2 个答案:

答案 0 :(得分:0)

您可以使用difference功能来实现:

game_options = ['ps2', 'pc'];
request = ['ps2', 'pc', 'xbox'];

console.log(_.difference(game_options, request).length > 0);

game_options = ['ps2', 'pc', 'xbox'];
request = ['ps2', 'pc'];

console.log(_.difference(game_options, request).length > 0);

答案 1 :(得分:0)

var isPresented = _.intersection(game_options, request).length === request.length