underscore.js 中的 uniq 不工作

uniq in underscore.js is not working

请检查此fiddleuniq()功能无效。尝试分离功能,但仍然无法解决。 JSON 先被压平然后我用 uniq.

来自 Underscore.js 文档:

Uniq
Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iteratee function.

所以你可以尝试使用函数将对象转换为可比较的值。例如:

var res = _.chain(data)
    .flatten()
    .uniq(function(v){ return v.mycount + v.mytype })
    .value();