如何制作一个新的数组嵌套排序图? [英] How do I make a new array nested sorted maps?
本文介绍了如何制作一个新的数组嵌套排序图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的数据块:
I have a block of data that looks like this:
var list = [
{ id: '1', title: 'thing1', week: 1, day: 1 },
{ id: '2', title: 'thing2', week: 1, day: 1 },
{ id: '3', title: 'thing3', week: 1, day: 2 },
{ id: '4', title: 'thing4', week: 1, day: 2 },
{ id: '5', title: 'thing5', week: 1, day: 3 },
{ id: '6', title: 'thing6', week: 1, day: 3 },
{ id: '7', title: 'thing7', week: 2, day: 4 },
{ id: '8', title: 'thing8', week: 2, day: 5 },
];
我想做的就是将这个List转换成可以输出以下内容的东西:
what I want to be able to do is to transform this List to something that outputs this:
var transformedList = [
{ 'Week 1': [
{ id: '1', title: 'thing1', day: 1, week: 1 },
{ id: '2', title: 'thing2', day: 1, week: 1 },
{ id: '3', title: 'thing3', day: 2, week: 1 },
{ id: '4', title: 'thing4', day: 2, week: 1 },
{ id: '5', title: 'thing5', day: 3, week: 1 },
{ id: '6', title: 'thing6', day: 3, week: 1 },
],
'Week 2': [
{ id: '7', title: 'thing7', day: 4, week: 2 },
{ id: '8', title: 'thing8', day: 5, week: 3 },
]
}
]
到目前为止,我尝试过的是:
What I've tried so far is:
var programMap = Map.fromIterable(userProgramSteps, key: (k) => k.getWeek(), value: (v) => v);
// handles the week port just fine, but very stuck on the list of days.
我对扑镖和飞镖很陌生,所以我有点迷路了……有人可以帮忙吗?
I'm very new to flutter and dart so I'm a little lost... could someone help?
推荐答案
请尊重您先前的(已删除)问题,此处的代码为:
Respect to your previous (deleted) question, the code here:
data_weeks.dart
const week_data = [
{ 'id': '1', 'week': 1, 'day': 1, 'title': 'thing1' },
{ 'id': '2', 'week': 1, 'day': 1, 'title': 'thing2' },
{ 'id': '3', 'week': 1, 'day': 1, 'title': 'thing3' },
{ 'id': '4', 'week': 1, 'day': 2, 'title': 'thing4' },
{ 'id': '5', 'week': 1, 'day': 2, 'title': 'thing5' },
{ 'id': '6', 'week': 2, 'day': 3, 'title': 'thing6' },
{ 'id': '7', 'week': 2, 'day': 4, 'title': 'thing7' },
];
home_page.dart
import 'package:flutter/material.dart';
import 'package:list_cards/src/shared/data_weeks.dart';
class HomePage extends StatelessWidget {
/* ---------------------------------------------------------------------------- */
const HomePage({Key key}) : super(key: key);
/* ---------------------------------------------------------------------------- */
@override
Widget build(BuildContext context) {
final data = getNewData();
return Scaffold(
appBar: AppBar(
title: Text('Hi!'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(10.0),
child: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) => ListTile(
title: Text(
'Week ${data.keys.toList()[index]}',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
subtitle: getSubtitles1(data[data.keys.toList()[index]], context),
),
),
),
);
}
Widget getSubtitles1(List<Map<int, Object>> data, BuildContext context) {
return Column(
children: List.generate(data.length, (index) => ListTile(
title: Text(
'DAY ${data[index].keys.toList()[0]}',
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black.withOpacity(0.4)
),
),
subtitle: getSubtitles2(data[index][data[index].keys.toList()[0]], context),
)),
);
}
Widget getSubtitles2(List<String> data, BuildContext context) {
final size = MediaQuery.of(context).size;
return Column(
children: List.generate(data.length, (index) => Card(
elevation: 0,
margin: EdgeInsets.all(0),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
child: Row(
children: [
Container(
width: (size.width + 80) / 2,
child: Text(data[index]),
),
IconButton(icon: Icon(Icons.check_box_outline_blank), onPressed: () {}),
],
),
),
)),
);
}
Map<int, Object> getNewData() {
var weeks = week_data.map<int>((m) => m['week']).toSet().toList();
var newData = Map<int, Object>();
weeks.forEach((w) {
newData[w] = week_data.where((m) => m['week'] == w).map<int>((m) => m['day']).toSet().map<Map<int, Object>>((e) => {e: week_data.where((m) => m['week'] == w && m['day'] == e).map<String>((m) => m['title']).toList()}).toList();
});
return newData;
}
}
结果:
关于您当前的问题, getNewData()
方法包含您的答案的逻辑.享受吧!
And with respect to your current question, getNewData()
method contains the logic for your answer. Enjoy it!
这篇关于如何制作一个新的数组嵌套排序图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文