Carl French
Carl French

Reputation: 27

How do you combine two arrays in Flutter?

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"},
  ];

Upvotes: 2

Views: 219

Answers (3)

Tim Brückner
Tim Brückner

Reputation: 2099

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.

Upvotes: 0

Robert Sandberg
Robert Sandberg

Reputation: 8645

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]}]

Upvotes: 2

Jungwon
Jungwon

Reputation: 1006

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]}]
 
}

Upvotes: 0

Related Questions