differenceWith<T> function

List<T> differenceWith<T>(
  1. List<T> list,
  2. List<List<T>> values,
  3. bool comparator(
    1. T,
    2. T
    )
)

This method is like difference except that it accepts comparator which is invoked to compare elements of list to values.

  • @param {List} list The list to inspect.
  • @param {List
  • @param {Function} comparator The comparator invoked per element.
  • @returns {List} Returns the new list of filtered values.

@example

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];

differenceWith(objects, [[{ 'x': 1, 'y': 2 }]], (a, b) => a['x'] == b['x'] && a['y'] == b['y']);
// => [{ 'x': 2, 'y': 1 }]

Implementation

List<T> differenceWith<T>(
  List<T> list,
  List<List<T>> values,
  bool Function(T, T) comparator,
) {
  if (list.isEmpty) {
    return [];
  }

  final exclusionList = <T>[];
  for (var arr in values) {
    exclusionList.addAll(arr);
  }

  return list.where((listValue) {
    return !exclusionList.any((excludeValue) => comparator(listValue, excludeValue));
  }).toList();
}