从数组的字典JS获取数据的问题

我在获取数组中存储的数据时遇到问题。我看不到函数的问题所在,为什么返回未定义的元素。

这是我在数组中存储数据的函数:

function getTaskKidData(str) {
   var tasks = $('#tasks_data > div');;
   var formated_tasks = [];
   var formated_kids = [];
   formated_homeworks = [];
   tasks.each(function(index) {
       var task_kids = $(this).find('ul').eq(0).find('li');
       var task_homeworks = $(this).find('ul').eq(1).find('li');
       if (task_kids.length > 0 && task_homeworks.length > 0) {
           task_kids.each(function(index) {
               var kid_name = $(this).text().trim();
               if (str == "kid"){
                   var kid = $('#kid_list > li > a[class*="active"]').text().replace(window.location.pathname.split('/')[2],'').trim();
                   if (kid == kid_name){
                       formated_kids.push({'name': kid_name});
                   }
               }else{
                   formated_kids.push({'name': kid_name});
               }
           });
           task_homeworks.each(function(index) {
               var homework_name = $(this).find('p').eq(0).text().trim();
               var homework_date = $(this).find('p').eq(1).text().trim();
               formated_homeworks.push({
                   'name': homework_name,
                   'date': homework_date,
               });
           });
           formated_tasks.push({
               'kids': task_kids,
               'homeworks': task_homeworks,
           })
       }
   });
   return formated_tasks;
}

I don't understand why the objects in the output of the array are "li" tags if I'm storing data as text. The output of the array is the next one: enter image description here This is the code where I'm trying to get the data:

var tasks = getTaskKidData("kid");
console.log(tasks)
       for (let i = 0; i < tasks.length; i++) {
           console.log("schedule");
           for (let j = 0; j < tasks[i]['kids'].length; j++) {
               console.log(tasks[i]['kids'][j]['name']);
           }
           for (let j = 0; j < tasks[i]['homeworks'].length; j++) {
               console.log(tasks[i]['homeworks'][j]['name']);
               console.log(tasks[i]['homeworks'][j]['date']);
           }
       }

这是我运行代码时的输出:

enter image description here

有什么问题的想法吗?

谢谢阅读!!

评论