Skip to content

Instantly share code, notes, and snippets.

@mandaputtra
Created June 20, 2023 09:14
Show Gist options
  • Save mandaputtra/a8714385b8baea97896e9cc3093bf489 to your computer and use it in GitHub Desktop.
Save mandaputtra/a8714385b8baea97896e9cc3093bf489 to your computer and use it in GitHub Desktop.
poetic-aqueduct-9454

poetic-aqueduct-9454

Created with <3 with dartpad.dev.

void main() {
// Ini adalah form question dan answer yang di isi oleh user
var listanswer = [
{ 'answer': 1, 'question': 1},
{ 'answer': 2, 'question': 2}
];
// ini data yang dikirim ke server
var dataDikirim = {
'ktp': 'ktp-kucing'
};
// Karena kirim ke server kita harus bikin object
// {
// ktp: 'ktp-kucing'
// questionId[1]: 1,
//. answerId[1]: 1,
// }
// questionId dan answerId ini dinamis dan dipisahkan dengan index
// mari kita bikin dulu
var mapped = {}; // tempat sementara buat nyimpan data question dan answer
for (var i = 0; i < listanswer.length; i++) {
// bikin key untuk Map
var keyOne = 'questionId[$i]';
var keyTwo = 'answerId[$i]';
// isi key dan value dari Map
mapped[keyOne] = listanswer[i]['question'];
mapped[keyTwo] = listanswer[i]['answer'];
}
// gabungin dataDikirim dan mapped, 2-2nya merupakan Map,
// kita bisa gabungin dengan ...
// cari saja di google "how to merge/combine map"
var merge = {
...dataDikirim,
...mapped
};
// Tada tergabung gaaaeeess!
print(merge);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment