How to split html code from a page into nodes

I want to read the html from a site and then split it into nodes. I tried this code:

function load() {
 $(document).ready(function () {
  $.get("https://example.com/index.html", function (data) {
   const loadpage = async function() {
    var nodes = [...data.childNodes].slice(-3);
    var cont = document.getElementById("container");
    var msg = nodes;

   });
    
        if(cont.innerHTML='') {
         cont.insertAdjacentHTML('afterbegin', msg);
    }   else {
        cont.innerHTML=msg;
     }          
    };
   
   loadpage();
   });
  });
}

load();

html looks like this:

<main>
 <div class="msg">something</div>
 <div class="msg">something</div>
 <div class="msg">something</div>
 <div class="msg">something</div>
 <div class="msg">something</div>
 <div class="msg">something</div>
</main>

the expected output should be:

 <div class="msg">something</div>
 <div class="msg">something</div>
 <div class="msg">something</div>

since I want only the last 3 nodes.

Thank you.

Converting ObjectList to Maplist for daily report in flutter

I Have created a method that converts TransactionList to MapList for report of daily income and daily expenses..

output like

[
{
‘date’:28-01-23,
‘totalIncome’:300.00,
‘totalExpense’:300.00,
‘balance’:0.00,

},
{
….
},
]

here are the data of Transaction


List<Transaction> transactions=[
  Transaction(date: DateTime(2023,01,18), isExpense: true, amount: 100.00,),
  Transaction(date: DateTime(2023,01,18), isExpense: true, amount: 200.00,),
  Transaction(date: DateTime(2023,01,18), isExpense: false, amount: 300.00,),
  Transaction(date: DateTime(2023,01,19), isExpense: false, amount: 200.00,),
  Transaction(date: DateTime(2023,01,19), isExpense: false, amount: 100.00,),
];


My method is working well but I think it is not proper coding, so I want to implement this method with some advance way….

List<Map<String, dynamic>> dailyreport() {
    
    //grouping data based on date field of transactions
    var maplist = groupBy(transactions, (Transaction element) => element.date);

    List<Map<String, dynamic>> reportlist = [];
    
    
    
    
    //loop for each key
    for (var x in maplist.keys) {


double sum_expenses = 0;
    double sum_income = 0;
      
      //getting transaction based on key value
      List<Transaction> trans = maplist[x] as List<Transaction>;
      for (int i = 0; i < trans.length; i++) {
        if (trans[i].isExpense)
          sum_expenses = sum_expenses + trans[i].amount;
        else
          sum_income = sum_income + trans[i].amount;
      }
      
      //adding map to reportlist
      reportlist.add({
        'date': x,
        'expenses': sum_expenses,
        'income': sum_income,
        'balance': sum_income - sum_expenses,
      });
    }
    return reportlist;
  }