当前位置:网站首页>根据日期重新排列数据js
根据日期重新排列数据js
2022-07-17 04:51:00 【接着奏乐接着舞。】
目录
需求:
将数据按照日期将原数组合并
源数据:
var arr = [
{ createTime: "2022-07-14 ", taskName: "完善个人信息", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: "首次下单", experiencePoints: '2' },
{ createTime: "2022-07-13 ", taskName: "每日签到", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: "每日签到", experiencePoints: '2' },
]要转换成的数据:
var arr2 = [
{
createTime: "2022-07-14 ",
list: [
{ taskName: "完善个人信息", experiencePoints: '2' },
{ taskName: "首次下单", experiencePoints: '2' },
{ taskName: "每日签到", experiencePoints: '2' },
],
},
{
createTime: "2022-07-13 ",
list: [
{ taskName: "每日签到", experiencePoints: '2' },
],
}
]实现思路:
1.一眼看去,是要按照日期进行数据划分的,那么第一件事就是数组去重,保留不重样的时期
2.接下来就是组装数据了,如果日期相等的话就添加进list
实现代码完整:
var arr = [ //初始代码
{ createTime: "2022-07-14 ", taskName: "完善个人信息", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: "首次下单", experiencePoints: '2' },
{ createTime: "2022-07-13 ", taskName: "每日签到", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: "每日签到", experiencePoints: '2' },
]
var arr2 = [ //目标代码
{
createTime: "2022-07-14 ",
list: [
{ taskName: "完善个人信息", experiencePoints: '2' },
{ taskName: "首次下单", experiencePoints: '2' },
{ taskName: "每日签到", experiencePoints: '2' },
],
},
{
createTime: "2022-07-13 ",
list: [
{ taskName: "每日签到", experiencePoints: '2' },
],
}
]
//逻辑部分
let demo3 = [...new Set(arr.map(item => item.createTime))] //['2022-07-14 ', '2022-07-13 ']
let end = []
demo3.forEach((i, o) => {
end.push({ createTime: i, list: [] })
})
end.forEach((it, id) => {
arr.forEach((item, index) => {
if (it.createTime == item.createTime) {
it.list.push({ taskName: item.taskName, experiencePoints: item.experiencePoints })
}
})
})
console.log(end, '------------------------------')边栏推荐
- MySQL InnoDB transaction related records
- Phthon3 install mysqldb error problem solving reason: image not found
- 赚钱大师小程序【最新版5.9.9】商城/佣金即时提现/分销推广/话费充值/美团饿了么外卖
- Minio installation, deployment and use
- LeetCode之最大正方形(暴力求解和动态规划求解)
- 基于JIRA7.9.2定制修改
- 对于每一个 Provider 实例都会维护多个连接
- 数据库与开源的未来
- 【TA-霜狼_may-《百人计划》】美术2.1 DCC工具链与引擎工具链
- masm32写程序
猜你喜欢

异或和加密方式的解密的复现

赚钱大师小程序【最新版5.9.9】商城/佣金即时提现/分销推广/话费充值/美团饿了么外卖

Extreme video compression using the pellet toolbox

Constraints on MySQL tables (Basics)
![Fudan micro fmql (domestic zynq) [PS of IAR bare metal development] - non byte aligned access](/img/e7/8349da2c240ac8f51524e5e51bd4d6.png)
Fudan micro fmql (domestic zynq) [PS of IAR bare metal development] - non byte aligned access

Pytorch image models (Timm) library

VirtualLab基础实验教程-7.偏振(3)

PowerDesigner显示Comment注释

Real time Bi (IV) low cost data quasi real time processing idea

MySql 一行变多行(根据特定符号分割)
随机推荐
高仿网易云音乐UI的微信小程序源码
异或和加密方式的解密的复现
Wkwebview sets the correct posture of custom useragent
【TA-霜狼_may-《百人计划》】基础渲染光照介绍(一)
Practice and improvement of children's network protection industry based on mobile Internet application
Gin框架原理
Overview of Baidu map technology, and application development of basic API and webapi
TiDB学习笔记【初阶】
Pytorch image models (Timm) library
数据库与开源的未来
PyTorch Image Models(timm)库
EasyExcel简单使用
MySQL one line to many lines (split according to specific symbols)
OpenLDAP自定义schema
On the third day of security, iptables prevents nmap scanning and binlog
策略模式代替if-else
AutoJs学习-2048小游戏自动化
PowerDesigner显示Comment注释
MySQL中判断和向下取整的使用场景和用法
EMQX 压力测试踩得坑供大家参考