Mark
Mark

Reputation: 3859

How can you combine a subset of two nested maps using a key?

Background

I'm trying to combine two maps (actually 2+) which are coming in from separate requests so that I can have one giant map that is fed into an infinite scroll list

My Question

Is it possible to combine data from two separate Map<String,dynamic>, in this case Map1 and Map2 into one single entity? With the catch being that only the data from a nested element is merged into the other, in this case from the appData field

Map1

{
   userData: 
         {loggedin: 0, username: Mike}, 
   appData: 
       [
         {id: 1, title: Title1, excerpt: , thumb: thumb1.jpg, threadid: 1, fid: 1, commentcount: 1, postdate: 1}, 
         {id: 2, title: 2, excerpt: , thumb: 2.jpg, threadid: 2, fid: 2, commentcount: 2, postdate: 2}, 
         {id: 3, title: 3, excerpt: , thumb: 3.jpg, threadid: 3, fid: 3, commentcount: 3, postdate: 3}
       ]
}

Map2

{
   userData: 
         {loggedin: 0, username: Mike}, 
   appData: 
       [
         {id: 4, title: Title4, excerpt: , thumb: thumb4.jpg, threadid: 4, fid: 4, commentcount: 4, postdate: 4}, 
         {id: 5, title: 5, excerpt: , thumb: 5.jpg, threadid: 5, fid: 5, commentcount: 5, postdate: 5}, 
         {id: 6, title: 6, excerpt: , thumb: 6.jpg, threadid: 6, fid: 6, commentcount: 6, postdate: 6}
       ]
}

Combined Map

{
   userData: 
         {loggedin: 0, username: Mike}, 
   appData: 
       [
         {id: 1, title: Title1, excerpt: , thumb: thumb1.jpg, threadid: 1, fid: 1, commentcount: 1, postdate: 1}, 
         {id: 2, title: 2, excerpt: , thumb: 2.jpg, threadid: 2, fid: 2, commentcount: 2, postdate: 2}, 
         {id: 3, title: 3, excerpt: , thumb: 3.jpg, threadid: 3, fid: 3, commentcount: 3, postdate: 3}
         {id: 4, title: Title4, excerpt: , thumb: thumb4.jpg, threadid: 4, fid: 4, commentcount: 4, postdate: 4}, 
         {id: 5, title: 5, excerpt: , thumb: 5.jpg, threadid: 5, fid: 5, commentcount: 5, postdate: 5}, 
         {id: 6, title: 6, excerpt: , thumb: 6.jpg, threadid: 6, fid: 6, commentcount: 6, postdate: 6}
       ]
}

Upvotes: 0

Views: 295

Answers (1)

mmcdon20
mmcdon20

Reputation: 6736

You could write a function which combines two maps like this:

Map<String, dynamic> combineAppData(Map<String, dynamic> a, Map<String, dynamic> b) =>
    {
      // copy all key/value pairs from map a
      ...a,
      // overwrite the appData key with a combination of items from both maps
      'appData': [
        ...?a['appData'],
        ...?b['appData'],
      ],
    };

If you want to merge 2+ maps in a single function, you could do it like this:

Map<String, dynamic> combineMultipleAppData(
        Map<String, dynamic> base, List<Map<String, dynamic>> merge) =>
    {
      // copy all key/value pairs from map a
      ...base,
      // overwrite the appData key with a combination of items from all maps
      'appData': [
        ...?base['appData'],
        for (final map in merge) ...?map['appData'],
      ],
    };

Upvotes: 2

Related Questions