0

How to convert this below array:

[{timeNameID: [2, 4, 5, 6, 7, 8], alarmIDAdzan: [4, 5, 5, 5, 5, 5], alarmIDIqamah: [5, 5, 4, 4, 4, 4]}]

to be:

[
  { timeNameID: '2', alarmIDAdzan: '4', alarmIDIqamah: '5' },
  { timeNameID: '4', alarmIDAdzan: '5', alarmIDIqamah: '5' },
  { timeNameID: '5', alarmIDAdzan: '5', alarmIDIqamah: '4' },
  { timeNameID: '6', alarmIDAdzan: '5', alarmIDIqamah: '4' },
  { timeNameID: '7', alarmIDAdzan: '5', alarmIDIqamah: '4' },
  { timeNameID: '8', alarmIDAdzan: '5', alarmIDIqamah: '4' }
]

Here is my code so far:

var makeArray = [
  {
    'timeNameID': selectedTimeNameID,
    'alarmIDAdzan': selectedAlarmAdzanID,
    'alarmIDIqamah': selectedAlarmIqamahID
  }
];

Is there any way to do the convert?

1 Answer 1

1

You could try this

  var a = [{"timeNameID": [2, 4, 5, 6, 7, 8], "alarmIDAdzan": [4, 5, 5, 5, 5, 5], "alarmIDIqamah": [5, 5, 4, 4, 4, 4]}];
  var b = Iterable<int>.generate(a[0]["timeNameID"]!.length).map((e) =>
      {"timeNameID":a[0]["timeNameID"]![e],"alarmIDAdzan":a[0]["alarmIDAdzan"]![e],"alarmIDIqamah":a[0]["alarmIDIqamah"]![e]});
  print(b);

this prints

({timeNameID: 2, alarmIDAdzan: 4, alarmIDIqamah: 5}, {timeNameID: 4, alarmIDAdzan: 5, alarmIDIqamah: 5}, {timeNameID: 5, alarmIDAdzan: 5, alarmIDIqamah: 4}, ..., {timeNameID: 7, alarmIDAdzan: 5, alarmIDIqamah: 4}, {timeNameID: 8, alarmIDAdzan: 5, alarmIDIqamah: 4})

Note thought, that this requires the fields to exist and all be the same length. It will lead to errors if it's not the case

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, is it possible to get the result with [{ the code }] not like this ({ the code }) ?
@HiDayurieDave yes, you can try to add .toList() to the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.