Home > Software design >  Transform a list of list architecture: [[1,2],[3,4]] -> [[1,3],[2,4]]
Transform a list of list architecture: [[1,2],[3,4]] -> [[1,3],[2,4]]

Time:01-16

How can I transform [[1,2],[3,4]] into [[1,3],[2,4]] in Dart?

CodePudding user response:

Assuming your question is to gather all the first elements of the list to a new list and all the last elements of the list into another list.

You could use the following code:

 const presentList = [[1, 2],[3, 4]];
 final updatedList = [presentList.map((e) => e.first).toList(), presentList.map((e) => e.last).toList()];

Now the content in the updatedList is [[1,3][2,4]]

CodePudding user response:

Your question should be detailled. You should generalize the logic behind.

In your specific case, a solution could be :

 const myList = [[1, 2],[3, 4]];
 final newList = [myList.map((e) => e.first).toList(), myList.map((e) => e.last).toList()];
         
  • Related