英文:
How do you combine two arrays in Flutter?
问题
给定上面的数组,我想将具有相同 docNo
但不同 objName
值的对象合并在一起。
期望的结果如下:
var list = [
{"docNo": "2023-12", "objName": ["img1", "img2"]},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
英文:
var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
Given an array like above, I want to combine the object with same docNo
with different value of ObjName
Expected result:
var list = [
{"docNo": "2023-12"", "objName": ["img1", "img2"]},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
答案1
得分: 2
以下是翻译好的代码部分:
import 'package:collection/collection.dart';
var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
var newObjects = <Map<String, dynamic>>[];
groupBy(list, (p0) => p0['docNo']).values.forEach(
(groupList) => newObjects.add({
'docNo': groupList.first['docNo'],
'objName': groupList.map((e) => e['objName']).toList(),
}),
);
print(newObjects);
将会输出:
[
{"docNo": "2023-12", "objName": ["img1", "img2"]},
{"docNo": "2022-10", "objName": ["img3"]},
{"docNo": "2022-08", "objName": ["img4"]}
]
英文:
Here is an alternative:
import 'package:collection/collection.dart';
var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
var newObjects = <Map<String, dynamic>>[];
groupBy(list, (p0) => p0['docNo']).values.forEach(
(groupList) => newObjects.add({
'docNo': groupList.first['docNo'],
'objName': groupList.map((e) => e['objName']).toList(),
}),
);
print(newObjects)
Will print:
[{docNo: 2023-12, objName: [img1, img2]}, {docNo: 2022-10, objName: [img3]}, {docNo: 2022-08, objName: [img4]}]
答案2
得分: 0
I'm sure there are better and simpler ways, but it should get you the results you want.
It is solved by storing the key values separately in a List, comparing them, and combining them. If you come across a more concise and better code, please share!
import 'dart:core';
var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
void main(){
List<String> temp = [];
List<Map<String, dynamic>> result = [];
for(int i=0; i<list.length; i++){
temp.add(list[i]["docNo"]!);
}
var keys = temp.toSet().toList();
List values = [];
for(int i=0; i<keys.length; i++){
for(int j=0; j<list.length; j++){
if(keys[i].toString() == list[j]["docNo"]!.toString()){
values.add(list[j]["objName"]!);
}
}
result.add(
{
"docNo" : keys[i],
"objName" : values.toList(),
}
);
values.clear();
}
print(result);
//[{docNo: 2023-12, objName: [img1, img2]},
//{docNo: 2022-10, objName: [img3]},
//{docNo: 2022-08, objName: [img4]}]
}
英文:
I'm sure there are better and simpler ways, but it should get you the results you want.
It is solved by storing the key values separately in a List, comparing them, and combining them. If you come across a more concise and better code, please share!
import 'dart:core';
var list = [
{"docNo": "2023-12", "objName": "img1"},
{"docNo": "2023-12", "objName": "img2"},
{"docNo": "2022-10", "objName": "img3"},
{"docNo": "2022-08", "objName": "img4"},
];
void main(){
List<String> temp = [];
List<Map<String, dynamic>> result = [];
for(int i=0; i<list.length; i++){
temp.add(list[i]["docNo"]!);
}
var keys = temp.toSet().toList();
List values = [];
for(int i=0; i<keys.length; i++){
for(int j=0; j<list.length; j++){
if(keys[i].toString() == list[j]["docNo"]!.toString()){
values.add(list[j]["objName"]!);
}
}
result.add(
{
"docNo" : keys[i],
"objName" : values.toList(),
}
);
values.clear();
}
print(result);
//[{docNo: 2023-12, objName: [img1, img2]},
//{docNo: 2022-10, objName: [img3]},
//{docNo: 2022-08, objName: [img4]}]
}
答案3
得分: 0
这是您要的代码翻译部分:
void main() {
final list = [
{'docNo': '2023-12', 'objName': 'img1'},
{'docNo': '2023-12', 'objName': 'img2'},
{'docNo': '2022-10', 'objName': 'img3'},
{'docNo': '2022-08', 'objName': 'img4'},
];
// 将所有的文档编号收集到一个集合中,以便可以按它们进行分组
final docNos = list.map((entry) => entry['docNo']).toSet();
// 构建一个结果列表,每个文档编号对应一个映射
final result = <Map<String, dynamic>>[];
for (final docNo in docNos) {
result.add({
'docNo': docNo,
'objName': list
.where((element) => element['docNo'] == docNo)
.map((docEntry) => docEntry['objName'])
.toList()
});
}
print(result);
}
不同之处在于,这个解决方案始终将objName
元素收集在一个列表中,即使对于docNo
只有一个条目。
英文:
A very simple approach would be:
void main() {
final list = [
{'docNo': '2023-12', 'objName': 'img1'},
{'docNo': '2023-12', 'objName': 'img2'},
{'docNo': '2022-10', 'objName': 'img3'},
{'docNo': '2022-08', 'objName': 'img4'},
];
// collects all doc numbers in a set, so you can group by them
final docNos = list.map((entry) => entry['docNo']).toSet();
// builds a result list, containing a map for each docNo
final result = <Map<String, dynamic>>[];
for (final docNo in docNos) {
result.add({
'docNo': docNo,
'objName': list
.where((element) => element['docNo'] == docNo)
.map((docEntry) => docEntry['objName'])
.toList()
});
}
print(result);
}
There is one difference, though. This solution collects objName
elements always in a list, even if there is only one entry for a docNo
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论