I have a list of lists
List<List<double>> data = [[1,2,3],[1,2,3], [2,2,3], [2,2,3], [1,2,3], ...];
I need to put the first element in the inner arrays in an array;
so result will be
<List<double> newData = [1,1,2,2,1.....]
CodePudding user response:
If you want just the first element for each inner list, or you want one element in particular, try this.
final data = [
[1, 2, 3],
[4, 5, 6]
];
final newData = [...data.map((e) => e.first)]; // or elementAt(...)
print(newData); // [1, 4]
If you want every element in that array (column-like), then I'll just try and brute force as follows (I'm sure there are better ways to do this with package:collection
, but I'm unsure how atm).
import 'package:collection/collection.dart';
const columnsAmount = 3;
final data = [
[1, 2, 3],
[4, 5, 6]
];
final result = <int, List<int>>{
for (var i = 0; i < columnsAmount; i ) i: []
};
for (final d in data) {
d.forEachIndexed((index, element) {
result[index]?.add(element);
});
}
print(result); // {0: [1, 4], 1: [2, 5], 2: [3, 6]}