您认为使用lodash实用程序编写函数以便检查数组是否存在重复值时,您认为最好(最好可以解释为最可读或最高效,您的选择)的方法.
我想输入['foo', 'foo', 'bar']并返回函数true.并输入['foo', 'bar', 'baz']并具有返回功能false.
我正在使用lodash并且我有一个数组:
const arr = ['firstname', 'lastname', 'initials', 'initials'];
Run Code Online (Sandbox Code Playgroud)
我想要一个仅包含出现多次的值(重复值)的新数组。
看起来 lodash 可能有一种特定的方法,但我看不到。像这样的东西:const dups = _.duplicates(arr);那就太好了。
我有:
// object with array values and number of occurrences
const counts = _.countBy(arr, value => value);
// reduce object to only those with more than 1 occurrence
const dups = _.pickBy(counts, value => (value > 1));
// just the keys
const keys = _.keys(dups);
console.log(keys); // ['initials']
Run Code Online (Sandbox Code Playgroud)
还有比这更好的方法吗..?