underscore.js - 是否有一个函数产生一个数组,该数组是两个数组的差异? [英] underscore.js - Is there a function that produces an array thats the difference of two arrays?

查看:107
本文介绍了underscore.js - 是否有一个函数产生一个数组,该数组是两个数组的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在underscore.js中寻找一个函数,该函数需要2个数组并返回一个新的唯一值数组?像_without

Looking for a function in underscore.js that will take 2 arrays and return a new array of unique values? Something like _without

_.without([0, 1, 3, 9], [1, 3]);

我想 => [0,9] 返回

看来_without的第二个arg是一个值列表,而不是一个数组。如果下划线具有我正在寻找的特定功能,那么任何人都知道
?或者我可以使用exisitng数组并将其转换为函数所需的值。

It appears _without's 2nd arg is a list of values, not an array. Anyone out there know if underscore has the specific function I'm looking for? Or can I take an exisitng array and covert it to values the function expects.

谢谢,

~ck in San Diego

Thanks,
~ck in San Diego

推荐答案

_。without.apply(_,[arr1] .concat(arr2))

[[0,1,3,9]]。concat([1,3]) [[0,1,3,9],1,3] ;

_ .without.apply(_,[[0,1,3,9,1,3]) _。没有([0,1,3,9] ,1,3)

你有一个非常好的 _。没有方法。因此,只需将数组转换为可以传递给函数的值列表。这是 Function.prototype.apply

You've got a perfectly good _.without method. So just convert an array into a list of values you can pass into a function. This is the purpose of Function.prototype.apply

这篇关于underscore.js - 是否有一个函数产生一个数组,该数组是两个数组的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆