30秒学会 Dart 片段 · 2019年7月20日

30秒学会 Dart 片段 – intersectionBy

Returns a list of elements that exist in both lists, after applying the provided function to each element of both.

Use Iterable.toSet() and Iterable.map() to get the unique values in b after applying fn to them.
Use Iterable.map() to apply fn to all the values of a, Iterable.toSet(), Iterable.where() and Iterable.contains() to keep only the values in the resulting list contained in the unique mapped values of b, Iterable.toList() to return the appropriate result.

代码实现

List<Y> intersectionBy<T, Y>(Iterable<T> a, Iterable<T> b, Y Function(T) fn) {
  final s = b.map(fn).toSet();
  return a.toSet().map((x) => fn(x)).where((x) => s.contains(x)).toList();
}

使用样例

intersectionBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], (v) => v['x']); // [1]