slice method

List slice(
  1. List array,
  2. List<int> row_index, [
  3. List<int>? column_index
])

Function used to slice two-dimensional arrays

slice(parent array, row_index start, stop, column_index start, stop)

var array = [[1, 2, 3, 4, 5],[6, 7, 8, 9, 10]];

print(m2d.slice(array, [0, 2], [1, 4]))

// output  [[2,3,4], [7,8,9]]

Implementation

List slice(List<dynamic> array, List<int> row_index,
    [List<int>? column_index]) {
  var result = [];
  // convert List<dynamic> to List<List>
  var arr = array.map((e) => e is List ? e : [e]).toList();
  try {
    if (row_index.length > 2) {
      throw Exception(
          'row_index only containing the elements between start and end.');
    }
    int rowMin = row_index[0];
    int rowMax = row_index[1];
    int counter = 0;
    arr.forEach((List row) {
      if (rowMin <= counter && counter < rowMax) {
        if (column_index != null && column_index.length > 1) {
          result.add(row.getRange(column_index[0], column_index[1]).toList());
        } else if (column_index == null) {
          result.add(row);
        } else {
          if (result.isEmpty) {
            result = [row[column_index[0]]];
          } else {
            result.add(row[column_index[0]]);
          }
        }
      }
      counter++;
    });
    return result;
  } catch (e) {
    throw new Exception(e);
  }
}