compact<T> function

List<T> compact<T>(
  1. List<T> list
)

Creates an list of elements where the values of the list are not Falsey.

Avoid calling it on fixed-length list.

// It alters the list object if the list is not fixed-length list.
var list = ['a', null, '', false, 'b'];
list.compact(); // ['a', 'b'];

// It returns new Object of compacted data;
var list = ['a', null, '', false, 'b'];

// here the list object is not altered
var compactedData_new_object = compact(list); // ['a', 'b'];

Implementation

List<T> compact<T>(List<T> list) {
  var result = <T>[];
  for (var value in list) {
    if (!isFalsey(value)) {
      result.add(value);
    }
  }
  return result;
}