Skip to content

Instantly share code, notes, and snippets.

@tejaswini-dev-techie
Last active August 2, 2024 10:46
Show Gist options
  • Select an option

  • Save tejaswini-dev-techie/6eb6802f696493c0f88031389f884bfb to your computer and use it in GitHub Desktop.

Select an option

Save tejaswini-dev-techie/6eb6802f696493c0f88031389f884bfb to your computer and use it in GitHub Desktop.
Extracting a limited number of videos data from Sample List Data
void main() {
int limit = 7;
List<Map<String, dynamic>> sampleListData = [
{
"module_id": "7",
"module_title": "Introduction",
"videos": [
{"sn_no": 1, "sno": "01", "video_id": "1668"},
{"sn_no": 2, "sno": "02", "video_id": "4"},
{"sn_no": 3, "sno": "03", "video_id": "38"},
{"sn_no": 4, "sno": "04", "video_id": "39"}
]
},
{
"module_id": "24",
"module_title": "Learn",
"videos": [
{"sn_no": 5, "sno": "05", "video_id": "37"},
{"sn_no": 6, "sno": "06", "video_id": "36"},
{"sn_no": 7, "sno": "07", "video_id": "35"},
]
},
{
"module_id": "25",
"videos": [
{"sn_no": 8, "sno": "08", "video_id": "34"},
{"sn_no": 9, "sno": "09", "video_id": "2"},
{"sn_no": 10, "sno": "10", "video_id": "6"}
]
}
];
List<Map<String, dynamic>> newList = [];
int count = 0;
for (var module in sampleListData) {
if (count >= limit) break;
List<Map<String, dynamic>> videos = [];
for (var video in module['videos']) {
if (count >= limit) break;
videos.add(video);
count++;
}
Map<String, dynamic> newModule = {
"module_id": module['module_id'],
"module_title": module['module_title'],
"videos": videos,
};
newList.add(newModule);
}
print(newList);
}
@tejaswini-dev-techie
Copy link
Author

This Dart code filters videos from a list of sample lists data, ensuring that no more than a specified limit of videos is included in the result.

Outcome

trimmed_list_outcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment