skip to Main Content

I have two arrays from api: data[‘list’] and dataStates[‘list’]

data['list'] = 
[
  {externalSessionId: 49b84a9d-984c-4086-bd9b-19418a4c01ad, score: 43, iapf: 13, iaf: 10.124789237976074, startTime: 2022-12-20T08:36:15.719Z, endTime: 2022-12-20T08:40:55.509Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a49, score: 50, iapf: 12, iaf: 11.1, startTime: 2022-12-20T09:00:57.244Z, endTime: 2022-12-20T09:10:57.244Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a45, score: 50, iapf: 9, iaf: 8.1, startTime: 2022-12-20T10:00:57.244Z, endTime: 2022-12-20T10:10:57.244Z},
  {externalSessionId: 1d6657db-5d9f-464a-b141-922fd1341fc5, score: 47, iapf: 10.199999809265137, iaf: 10.19788932800293, startTime: 2022-12-20T10:27:40.590Z, endTime: 2022-12-20T12:14:58.341Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a46, score: 50, iapf: 10, iaf: 9.1, startTime: 2022-12-20T11:00:57.244Z, endTime: 2022-12-20T11:10:57.244Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a47, score: 50, iapf: 11, iaf: 10.1, startTime: 2022-12-20T12:00:57.244Z, endTime: 2022-12-20T12:10:57.244Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a48, score: 50, iapf: 12, iaf: 10.1, startTime: 2022-12-20T13:00:57.244Z, endTime: 2022-12-20T13:10:57.244Z},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a41, score: 50, iapf: 12.2, iaf: 12.1, startTime: 2022-12-20T19:00:57.244Z, endTime: 2022-12-20T19:10:57.244Z}
]

And

dataStates['list'] = 
[
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a49, iaf: 11.1, iapf: 12, startTime: 2022-12-20T09:00:57.244Z, endTime: 2022-12-20T09:10:57.244Z, states: {Involvement: 2, Chronic Fatigue: 2, Stress: 2, Anxiety: 1, Relaxation: 2}},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a45, iaf: 8.1, iapf: 9, startTime: 2022-12-20T10:00:57.244Z, endTime: 2022-12-20T10:10:57.244Z, states: {Stress: 2, Relaxation: 1, Anxiety: 1, Involvement: 1, Chronic Fatigue: 2}},
  {externalSessionId: 1d6657db-5d9f-464a-b141-922fd1341fc5, iaf: 10.19788932800293, iapf: 10.199999809265137, startTime: 2022-12-20T10:27:40.590Z, endTime: 2022-12-20T12:14:58.341Z, states: {Relaxation: 23, Involvement: 3, SlightFatigue: 18}},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a46, iaf: 9.1, iapf: 10, startTime: 2022-12-20T11:00:57.244Z, endTime: 2022-12-20T11:10:57.244Z, states: {Stress: 2, Chronic Fatigue: 2, Relaxation: 1, Involvement: 2, Anxiety: 1}},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a47, iaf: 10.1, iapf: 11, startTime: 2022-12-20T12:00:57.244Z, endTime: 2022-12-20T12:10:57.244Z, states: {Involvement: 1, Chronic Fatigue: 2, Stress: 2}},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a48, iaf: 10.1, iapf: 12, startTime: 2022-12-20T13:00:57.244Z, endTime: 2022-12-20T13:10:57.244Z, states: {Chronic Fatigue: 4, Involvement: 1, Relaxation: 1, Anxiety: 1, Stress: 2}},
  {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a41, iaf: 12.1, iapf: 12.2, startTime: 2022-12-20T19:00:57.244Z, endTime: 2022-12-20T19:10:57.244Z, states: {Chronic Fatigue: 2, Stress: 2, Involvement: 2, Relaxation: 1}}
]

I want to go through data[‘list’] and take externalSessionId of data[‘list’] and dataStates[‘list’]

If I find externalSessionId inside dataStates[‘list’] then I stick together elements with identical externalSessionId inside new Array(f.e stickedArray)

And new element should look like this:

{externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a49, score: 50, iapf: 12, iaf: 11.1, startTime: 2022-12-20T09:00:57.244Z, endTime: 2022-12-20T09:10:57.244Z, states: {Involvement: 2, Chronic Fatigue: 2, Stress: 2, Anxiety: 1, Relaxation: 2}}

But if I couldn’t find the same externalSessionId inside dataStates then I set states: null, like this:

{externalSessionId: 49b84a9d-984c-4086-bd9b-19418a4c01ad, score: 43, iapf: 13, iaf: 10.124789237976074, startTime: 2022-12-20T08:36:15.719Z, endTime: 2022-12-20T08:40:55.509Z, states: null},

How can I do this?

3

Answers


  1. You can try this:

    List<Map<String, dynamic>> result = [];
    
    for (Map<String, dynamic> element in listData) {
      for (Map<String, dynamic> item in dataStatesList) {
        if (item["externalSessionId"] == element["externalSessionId"]) {
          if (item["states"] != null) {
            element["states"] = item["states"];
            result.add(element);
          } else {
            result.add(element);
          }
        }
      }
    }
    

    listData is your data['list'] and dataStatesList is your dataStates['list'].

    result output:

    [
     {
       externalSessionId: 49b84a9d-984c-4086-bd9b-19418a4c01ad, 
       score: 43, iapf: 13, iaf: 10.124789237976074, 
       startTime: 2022-12-20T08:36:15.719Z, 
       endTime: 2022-12-20T08:40:55.509Z
     }, 
     {
       externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a49, 
       score: 50, iapf: 12, iaf: 11.1, 
       startTime: 2022-12-20T09:00:57.244Z, 
       endTime: 2022-12-20T09:10:57.244Z, 
       states: {
           Involvement: 2, Chronic Fatigue: 2, Stress: 2, Anxiety: 1, Relaxation: 2}
     }, 
     ...
    ]
    
    Login or Signup to reply.
  2. I believe this might work:

    final result = [
      for (Map x in data['list'])
        {
          ...x,
          ...(dataStates['list'] as List).firstWhereOrNull(
                  (y) => x['externalSessionId'] == y['externalSessionId']) ??
              {}
        }
    ];
    
    Login or Signup to reply.
  3. You could use singleWhere with orElse: return null.

    final newDataListWithStates = dataList.map((dataListElement) {
        var copy = Map.from(dataListElement);
        copy["states"] = dataStatesList.singleWhere(
            (element) =>
                element["externalSessionId"] ==
                dataListElement["externalSessionId"],
            orElse: () => {"states": null})["states"];
        return copy;
      }).toList();
    

    output:

    [
    {externalSessionId: 49b84a9d-984c-4086-bd9b-19418a4c01ad, score: 43, iapf: 13, iaf: 10.124789237976074, startTime: 2022-12-20T08:36:15.719Z, endTime: 2022-12-20T08:40:55.509Z, states: null}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a49, score: 50, iapf: 12, iaf: 11.1, startTime: 2022-12-20T09:00:57.244Z, endTime: 2022-12-20T09:10:57.244Z, states: {Involvement: 2, Chronic Fatigue: 2, Stress: 2, Anxiety: 1, Relaxation: 2}}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a45, score: 50, iapf: 9, iaf: 8.1, startTime: 2022-12-20T10:00:57.244Z, endTime: 2022-12-20T10:10:57.244Z, states: {Stress: 2, Relaxation: 1, Anxiety: 1, Involvement: 1, Chronic Fatigue: 2}}
    {externalSessionId: 1d6657db-5d9f-464a-b141-922fd1341fc5, score: 47, iapf: 10.199999809265137, iaf: 10.19788932800293, startTime: 2022-12-20T10:27:40.590Z, endTime: 2022-12-20T12:14:58.341Z, states: {Relaxation: 23, Involvement: 3, SlightFatigue: 18}}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a46, score: 50, iapf: 10, iaf: 9.1, startTime: 2022-12-20T11:00:57.244Z, endTime: 2022-12-20T11:10:57.244Z, states: {Stress: 2, Chronic Fatigue: 2, Relaxation: 1, Involvement: 2, Anxiety: 1}}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a47, score: 50, iapf: 11, iaf: 10.1, startTime: 2022-12-20T12:00:57.244Z, endTime: 2022-12-20T12:10:57.244Z, states: {Involvement: 1, Chronic Fatigue: 2, Stress: 2}}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a48, score: 50, iapf: 12, iaf: 10.1, startTime: 2022-12-20T13:00:57.244Z, endTime: 2022-12-20T13:10:57.244Z, states: {Chronic Fatigue: 4, Involvement: 1, Relaxation: 1, Anxiety: 1, Stress: 2}}
    {externalSessionId: 98b6b7dd-76a7-4b5a-abd8-61ecd1970a41, score: 50, iapf: 12.2, iaf: 12.1, startTime: 2022-12-20T19:00:57.244Z, endTime: 2022-12-20T19:10:57.244Z, states: {Chronic Fatigue: 2, Stress: 2, Involvement: 2, Relaxation: 1}}
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search