<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var?arr=[{createTime:"2022-04-28 00:00:00",id:1},{createTime:"2022-04-27 00:00:00",id:2},{createTime:"2022-04-27 00:00:00",id:3},{createTime:"2022-04-28 00:00:00",id:4},{createTime:"2022-04-27 00:00:00",id:5}
];
// 传入一个数组
// 按照特定方式格式化
console.log(group(arr))
?function group(arr) {
? ? return Object.values(arr.reduce((result, item) => {
? ? ? ? const {createTime} = item;
// console.log(createTime)
? ? ? ? const { values = [] } = result[createTime] || {};
? ? ? ? values.push(item);
? ? ? ? result[createTime] = { createTime, values };
? ? ? ? return result;
? ? }, {}));
}
</script>
</body>
</html>
?
|