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

Javascript 如何从dataTables中获取不同的值,并使用JS对特定字段的总数求和,javascript,jquery,arrays,ajax,datatables,Javascript,Jquery,Arrays,Ajax,Datatables,如何从数据表中获取不同的值。如下图所示 您将看到“课程1”具有相同的值。我想从“课程名称”中获取所有不同的值,同时使用JS从数据表中相同的不同值添加所有等效的“学生” 我希望回报是 课程1,4名学生 编辑: HTML代码: <table class="table" id="bookingReport" cellspacing="0" width="100%"> <thead class="thead-inverse"> <tr>

如何从数据表中获取不同的值。如下图所示

您将看到“课程1”具有相同的值。我想从“课程名称”中获取所有不同的值,同时使用JS从数据表中相同的不同值添加所有等效的“学生”

我希望回报是

课程1,4名学生

编辑:

HTML代码:

<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>

要计算分配给每个课程的人数,可以使用数组的
reduce
功能。给出以下学生数组,您可以轻松计算结果

var bookingList=[{
id:1,
名字:“爱丽丝”,
课程名称:“物理”
},
{
id:2,
姓名:'鲍勃',
课程名称:“物理”
},
{
id:3,
姓名:'艾米丽',
课程名称:“数学”
},
{
id:1,
名字:“爱丽丝”,
课程名称:“数学”
},
{
id:4,
姓名:'简',
课程名称:“生物学”
},
{
id:5,
名字:“丹”,
课程名称:“化学”
}
]
var结果=bookingList.reduce(函数(prevValue、currValue、索引、数组){
var bookingery=数组[索引]
if(prevValue[bookingEntry.courseName]==null){
prevValue[bookingEntry.courseName]=1
}否则{
prevValue[bookingEntry.courseName]++
}
返回值
}, {});

console.log(result)
统计分配给每个课程的人数,您可以使用数组的
reduce
功能。给出以下学生数组,您可以轻松计算结果

var bookingList=[{
id:1,
名字:“爱丽丝”,
课程名称:“物理”
},
{
id:2,
姓名:'鲍勃',
课程名称:“物理”
},
{
id:3,
姓名:'艾米丽',
课程名称:“数学”
},
{
id:1,
名字:“爱丽丝”,
课程名称:“数学”
},
{
id:4,
姓名:'简',
课程名称:“生物学”
},
{
id:5,
名字:“丹”,
课程名称:“化学”
}
]
var结果=bookingList.reduce(函数(prevValue、currValue、索引、数组){
var bookingery=数组[索引]
if(prevValue[bookingEntry.courseName]==null){
prevValue[bookingEntry.courseName]=1
}否则{
prevValue[bookingEntry.courseName]++
}
返回值
}, {});

console.log(result)
来自@t3mplar的优秀答案,这是一个使用模拟ajax的版本,它还考虑了同一课程在不同日期发生的可能性:

"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
    }, []);
}
“dataSrc”:函数(数据){
return data.reduce(函数(returnedArray、currentElement、index、originalArray){
让foundOne=returnedArray.findIndex(函数(元素){
返回currentElement.course===element.course&¤tElement.date===element.date
});
if(foundOne<0){
返回阵列({
“课程”:currentElement.course,
“日期”:currentElement.date,
“学生”:1
});
}否则{
returnedArray[foundOne]。学生++;
}
返回阵列
}, []);
}

使用JSFiddle。

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

"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
    }, []);
}
“dataSrc”:函数(数据){
return data.reduce(函数(returnedArray、currentElement、index、originalArray){
让foundOne=returnedArray.findIndex(函数(元素){
返回currentElement.course===element.course&¤tElement.date===element.date
});
if(foundOne<0){
返回阵列({
“课程”:currentElement.course,
“日期”:currentElement.date,
“学生”:1
});
}否则{
returnedArray[foundOne]。学生++;
}
返回阵列
}, []);
}

正在使用JSFIDLE。

请发布您的HTML和JS代码。@gyre:哎呀,我忘了,谢谢!。现在编辑它BTW请发布您的HTML和JS代码。@gyre:Opps我忘了,谢谢!。现在编辑它btwI获取值,但它返回0:课程:“课程1”日期:“2016年12月12日”学生:11:课程:“课程1”日期:“2016年12月12日”学生:12:课程:“课程1”日期:“2016年12月12日”学生:13:课程:“课程1”日期:“2016年12月12日”学生:1我不知道所附的代码有什么问题。请参阅更新我的JSFIDLE以使用reduce;-)我得到这些值,但它返回0:课程:“课程1”日期:“2016年12月12日”学生:1:课程:“课程1”日期:“2016年12月12日”学生:1 2:课程:“课程1”日期:“2016年12月12日”学生:1 3:课程:“课程1”日期:“2016年12月12日”学生:1我不知道所附的代码有什么问题。请参阅更新我的JSFIDLE以使用reduce;-)