当前位置:网站首页>Rearrange data according to date JS
Rearrange data according to date JS
2022-07-19 04:43:00 【Then play music and dance.】
Catalog
demand :
Merge the data into the original array according to the date
Source data :
var arr = [
{ createTime: "2022-07-14 ", taskName: " Improve personal information ", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: " Order for the first time ", experiencePoints: '2' },
{ createTime: "2022-07-13 ", taskName: " Check in every day ", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: " Check in every day ", experiencePoints: '2' },
]Data to be converted :
var arr2 = [
{
createTime: "2022-07-14 ",
list: [
{ taskName: " Improve personal information ", experiencePoints: '2' },
{ taskName: " Order for the first time ", experiencePoints: '2' },
{ taskName: " Check in every day ", experiencePoints: '2' },
],
},
{
createTime: "2022-07-13 ",
list: [
{ taskName: " Check in every day ", experiencePoints: '2' },
],
}
]Realize the idea :
1. In a glance , It is to divide the data according to the date , Then the first thing is to remove the duplicate of the array , Retain the period of no duplication
2. The next step is to assemble data , If the dates are equal, add list
Complete code :
var arr = [ // Initial code
{ createTime: "2022-07-14 ", taskName: " Improve personal information ", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: " Order for the first time ", experiencePoints: '2' },
{ createTime: "2022-07-13 ", taskName: " Check in every day ", experiencePoints: '2' },
{ createTime: "2022-07-14 ", taskName: " Check in every day ", experiencePoints: '2' },
]
var arr2 = [ // Object code
{
createTime: "2022-07-14 ",
list: [
{ taskName: " Improve personal information ", experiencePoints: '2' },
{ taskName: " Order for the first time ", experiencePoints: '2' },
{ taskName: " Check in every day ", experiencePoints: '2' },
],
},
{
createTime: "2022-07-13 ",
list: [
{ taskName: " Check in every day ", experiencePoints: '2' },
],
}
]
// Logical part
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, '------------------------------')边栏推荐
- [Vuforia] 图像识别的简单逻辑
- DNS原理及解析过程
- 智能硬件语音控制的时频图分类挑战赛2.0(思路以及结果,目前top5)
- WildFly:如何从位于另一个应用程序中的 EJB 调用 EJB
- Wildfly: how to call EJBs from EJBs located in another application
- Rk1126 realizes picture in picture function picture in picture for rk 1126
- Architecture and application of secure multiparty computing
- 【FPGA教程案例27】通过Verilog实现双口RAM乒乓结构
- As a result, all system modifications of the company will be upgraded
- PHP array_filter去空保留0,false
猜你喜欢

The author of surging issued the pressure test results

C语言动态内存开辟和柔性数组

MySQL one line to many lines (split according to specific symbols)

高仿网易云音乐UI的微信小程序源码

MySQL中判断和向下取整的使用场景和用法

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

PowerDesigner displays comment comments

快速掌握MIPI开发攻略

HighTec 新建 AURIX TC37X demo 工程

VirtualLab基础实验教程-7.偏振(3)
随机推荐
[Unity] Input.gettouch[index]的index
TiDB学习笔记【初阶】
Challenges and Countermeasures of deep forgery to national security
智能硬件语音控制的时频图分类挑战赛2.0(思路以及结果,目前top5)
万文多图之Word高效插入参考文献
记录一次存储过程批量修改表结构
Real time Bi (IV) low cost data quasi real time processing idea
Summary of black screen problems in unity UMP packaging
DNS原理及解析过程
kettle5.4问题记录
Leetcode remove element
B+ tree stored procedures, triggers, substring and substr, and truncate and delete
N-Beats模型于2020年发布,并且优于M4比赛的获胜者3%!
Reproduction of XOR and encryption decryption
Water and electricity meter reading and recharge management system in Colleges and universities in property community
golang反转reverse切片slice代码示例
Overview of Baidu map technology, and application development of basic API and webapi
Tidb learning notes [preliminary]
As a result, all system modifications of the company will be upgraded
Delete the file unable to find or create trash directory