I have a list model class name previous loan. I want to add loanOutstanding value in previousLoan Model if its key has matched. How can I do this?
"prevLoans": [
{
"id": 200855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 345855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 965821,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},
],
"loanOutstanding": {
"200855": 21379
},
CodePudding user response:
After parsing the json, you can iterate over the loan list and modify the matched one:
for (final loan in loans) {
final id = loan['id'].toString();
if (outstandings.containsKey(id)) {
loan['loanOutstanding'] = outstandings[id]!;
}
}
CodePudding user response:
as I found out from your explanation, and your Map, i added { } to your Map because your map is not complete in my idea:
var loansMap ={
"prevLoans": [
{
"id": 200855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 345855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 965821,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},
],
"loanOutstanding": {
"200855": 21379
},
};
here is the code to put 21379 as a value of loanOutstanding key in prevLoans Model:
for (var loan in loansMap['prevLoans'] as List) {
final id = loan['id'].toString();
if ((loansMap['loanOutstanding'] as Map).containsKey(id)) {
loan['loanOutstanding'] = (loansMap['loanOutstanding'] as Map)[id];
}
}
print('loans: ${loansMap}');
here is the print result:
loans: {
prevLoans: [
{
id: 200855,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000,
loanOutstanding: 21379
},
{
id: 345855,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000
},
{
id: 965821,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000
}
],
loanOutstanding: {200855: 21379}
}
as you see loanOutstanding: 21379
added.
if some of the structured changed is because you haven't added clear map in your explanation.
but if you have any question or help i'm here ;)
happy coding...