如何在Flutter中合并两个数组?

huangapple go评论66阅读模式
英文:

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 &#39;package:collection/collection.dart&#39;;

  var list = [
    {&quot;docNo&quot;: &quot;2023-12&quot;, &quot;objName&quot;: &quot;img1&quot;},
    {&quot;docNo&quot;: &quot;2023-12&quot;, &quot;objName&quot;: &quot;img2&quot;},
    {&quot;docNo&quot;: &quot;2022-10&quot;, &quot;objName&quot;: &quot;img3&quot;},
    {&quot;docNo&quot;: &quot;2022-08&quot;, &quot;objName&quot;: &quot;img4&quot;},
  ];

  var newObjects = &lt;Map&lt;String, dynamic&gt;&gt;[];
  groupBy(list, (p0) =&gt; p0[&#39;docNo&#39;]).values.forEach(
        (groupList) =&gt; newObjects.add({
          &#39;docNo&#39;: groupList.first[&#39;docNo&#39;],
          &#39;objName&#39;: groupList.map((e) =&gt; e[&#39;objName&#39;]).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 &#39;dart:core&#39;;

var list = [
    {&quot;docNo&quot;: &quot;2023-12&quot;, &quot;objName&quot;: &quot;img1&quot;},
    {&quot;docNo&quot;: &quot;2023-12&quot;, &quot;objName&quot;: &quot;img2&quot;},
    {&quot;docNo&quot;: &quot;2022-10&quot;, &quot;objName&quot;: &quot;img3&quot;},
    {&quot;docNo&quot;: &quot;2022-08&quot;, &quot;objName&quot;: &quot;img4&quot;},
  ];

void main(){
  List&lt;String&gt; temp = [];
  
  List&lt;Map&lt;String, dynamic&gt;&gt; result = [];
  
  for(int i=0; i&lt;list.length; i++){
    temp.add(list[i][&quot;docNo&quot;]!);
  }
  var keys = temp.toSet().toList();
  List values = [];
  
  for(int i=0; i&lt;keys.length; i++){
   
    for(int j=0; j&lt;list.length; j++){
      if(keys[i].toString() == list[j][&quot;docNo&quot;]!.toString()){
        values.add(list[j][&quot;objName&quot;]!);
      } 
    }
    result.add(
      {
        &quot;docNo&quot; : keys[i], 
        &quot;objName&quot; : 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 = [
    {&#39;docNo&#39;: &#39;2023-12&#39;, &#39;objName&#39;: &#39;img1&#39;},
    {&#39;docNo&#39;: &#39;2023-12&#39;, &#39;objName&#39;: &#39;img2&#39;},
    {&#39;docNo&#39;: &#39;2022-10&#39;, &#39;objName&#39;: &#39;img3&#39;},
    {&#39;docNo&#39;: &#39;2022-08&#39;, &#39;objName&#39;: &#39;img4&#39;},
  ];

  // collects all doc numbers in a set, so you can group by them
  final docNos = list.map((entry) =&gt; entry[&#39;docNo&#39;]).toSet();

  // builds a result list, containing a map for each docNo
  final result = &lt;Map&lt;String, dynamic&gt;&gt;[];
  for (final docNo in docNos) {
    result.add({
      &#39;docNo&#39;: docNo,
      &#39;objName&#39;: list
          .where((element) =&gt; element[&#39;docNo&#39;] == docNo)
          .map((docEntry) =&gt; docEntry[&#39;objName&#39;])
          .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.

huangapple
  • 本文由 发表于 2023年2月8日 15:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定