一、js数组分组
1.js对数据分组类似group by
源码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>jquery 数组按某一字段分组</title>
<!-- <script src="./Scripts/jquery-3.3.1.min.js"></script> -->
<script src="./Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function()
{
let a = null;
let aa = a?a:0;
console.log(aa);
let b = undefined;
let bb = b?b:0;
console.log(bb);
let c = '';
let cc = c?c:0;
console.log(cc);
list = [
{"name": "John", "Average": 15, "High": 10, "DtmStamp": 1358226000000},
{"name": "Jane", "Average": 16, "High": 92, "DtmStamp": 1358226000000},
{"name": "Jane", "Average": 17, "High": 45, "DtmStamp": 1358226000000},
{"name": "John", "Average": 18, "High": 87, "DtmStamp": 1358226000000},
{"name": "Jane", "Average": 15, "High": 10, "DtmStamp": 1358226060000},
{"name": "John", "Average": 16, "High": 87, "DtmStamp": 1358226060000},
{"name": "John", "Average": 17, "High": 45, "DtmStamp": 1358226060000},
{"name": "Jane", "Average": 18, "High": 92, "DtmStamp": 1358226060000}
];
const sorted =groupBy(list, function (item) {
return [item.name];//按照name进行分组
});
console.log(sorted);
})
function groupBy(array, f)
{
debugger;
const groups = {};
array.forEach(function (o) { //注意这里必须是forEach 大写
const group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function (group) {
return groups[group];
});
}
</script>
</body>
</html>
具体实现思路:
- 1.函数groupBy有两个形参,一为对象数组,二为匿名函数(该函数功能:返回对象的某个指定属性的属性值并存放在数组中);
- 2.groupBy函数内,先创建一个空对象;
- 3.然后forEach遍历对象数组,遍历时要执行的函数中只有一个形参o(数组中的每个元素);
- 4.由于下面函数调用是想用name来分组,因此let group = JSON.stringify( f(o) ),相当于先获取到对象数组list中的name属性对应的属性值并放入数组中:["John"],然后再将属性值转换为json字符串:'["John"]';
- 5.groups[group] = groups[group] || [],在js中对象也是关联数组,因此这里相当于做了两件事,一是把group作为groups的key,二是将对应的value初始化,第一次执行为空数组,循环执行时找到相同的name时保持不变;
- 6.groups[group].push( o ),这句相当于把list中每个对象压入数组中作为value;
- 7.最后,Object.keys(groups)是取出groups对象中的所有key,然后遍历一个个key组成的新数组,返回分好了组的二维数组
2.js根据json数据中的某一个属性来给数据分组
源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span id ="span" style=" 50px;height: 200px;"></span>
</body>
<script>
var arr = [{"Group":1,"Groupheader":"质量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"质量巡检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"设备管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备专业点检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备日检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备周检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备月检","Right":"","Rightimg":""}];
var map = {},
dest = [];
for(var i = 0; i < arr.length; i++){
var ai = arr[i];
if(!map[ai.Groupheader]){
dest.push({
Groupheader: ai.Groupheader,
data: [ai]
});
map[ai.Groupheader] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.Groupheader == ai.Groupheader){
dj.data.push(ai);
break;
}
}
}
}
console.log(JSON.stringify(dest));
var sapn = document.getElementById("span");
span.innerHTML = JSON.stringify(dest);
</script>
</html>
二、js数组排序
参考1:https://blog.csdn.net/shuoSmallWhite/article/details/89447418
参考2:https://blog.csdn.net/qq_24607837/article/details/83342585
总结如下:
/**
* 数组排序:Desc:降序 Asc:升序
* @param {any} property
*/
function arraySort(property,type) {
return (firstobj, secondobj) => {
const firstValue = firstobj[property];
const secondValue = secondobj[property];
switch (type) {
case "Desc":
return secondValue - firstValue; //降序
case "Asc":
return firstValue - secondValue; //升序
default:
return firstValue - secondValue; //升序
}
};
}