首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从dataTables中获取不同的值,并使用JS对总的特定字段求和

如何从dataTables中获取不同的值,并使用JS对总的特定字段求和
EN

Stack Overflow用户
提问于 2017-03-07 14:36:41
回答 2查看 957关注 0票数 0

如何从dataTables获取不同的值。你可以从下面的图片中看到

您将看到“Course 1”具有相同的值。我希望从“课程名称”中获得所有不同的值,同时使用JS在DataTables中从相同的不同值中添加所有等效的“学生”。

我想要的回报是

课程1,4名学生

编辑:

HTML代码:

代码语言:javascript
运行
复制
<table class="table" id="bookingReport" cellspacing="0" width="100%">
    <thead class="thead-inverse">
        <tr>
            <th><h4>Course Names</h4></th>
            <th><h4>Names</h4></th>
            <th><h4>Dates</h4></th>
        </tr>
    </thead>        
</table>

JS代码:

代码语言:javascript
运行
复制
"dataSrc": function(result) {
    var obj, id, paymentMethod;
    var validatedResult = [];

    $.each(result.aaData, function(index, value) {
        var givenName, surname, organisationName;

        id = value.id;
        dateCreated = value.dateCreated;



        $.each(value.bookingDetail, function(i, v) {


            $.each(v.student.studentCourseDetail, function(ii, sd) {
                obj = new Object();

                obj["id"] = sd.id;
                obj["surname"] = surname;
                obj["givenName"] = givenName;
                obj["dateCreated"] = dateCreated;
                obj["courseName"] = sd.courseName;


                validatedResult.push(obj);



            });




        });

    });

    return validatedResult;

}, },

"aoColumns": [{
        "data": "courseName"
    }, {
        "data": "givenName"
    }, {
        "data": "dateCreated"
    }

],
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-07 15:34:12

要统计分配给每个课程的人数,可以使用数组的reduce函数。给出下面的学生数组,你可以很容易地计算出结果。

代码语言:javascript
运行
复制
var bookingList = [{
    id: 1,
    name: 'Alice',
    courseName: 'Physics'
  },
  {
    id: 2,
    name: 'Bob',
    courseName: 'Physics'
  },
  {
    id: 3,
    name: 'Emily',
    courseName: 'Math'
  },
  {
    id: 1,
    name: 'Alice',
    courseName: 'Math'
  },
  {
    id: 4,
    name: 'Jane',
    courseName: 'Biology'
  },
  {
    id: 5,
    name: 'Dan',
    courseName: 'Chemistry'
  }
]

var result = bookingList.reduce(function(prevValue, currValue, index, array) {
  var bookingEntry = array[index]
  if (prevValue[bookingEntry.courseName] == null) {
    prevValue[bookingEntry.courseName] = 1
  } else {
    prevValue[bookingEntry.courseName]++
  }

  return prevValue
}, {});

console.log(result)

票数 1
EN

Stack Overflow用户

发布于 2017-03-07 15:51:26

来自@t3mplar的很好的答案,这是一个使用模拟ajax的版本,它还考虑了相同课程在不同日期发生的可能性:

代码语言:javascript
运行
复制
"dataSrc": function(data) {
    return data.reduce(function(returnedArray, currentElement, index, originalArray) {
        let foundOne = returnedArray.findIndex(function(element) {
            return currentElement.course === element.course && currentElement.date === element.date
        });
        if (foundOne < 0) {
            returnedArray.push({
                "course": currentElement.course,
                "date": currentElement.date,
                "students": 1
            });
        } else {
            returnedArray[foundOne].students++;
        }
        return returnedArray
    }, []);
}

Working JSFiddle here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42641771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档