1.将数组对象变成一个简单的装换
源数据
let arr = [
0: {amount: '12.00', category: '-', createdAt: '2022年5月3日', date: 1651567753661, note: '', }
1: {amount: '25.00', category: '-', createdAt: '2022年5月2日', date: 1651420800000, note: '', }
2: {amount: '21.00', category: '-', createdAt: '2022年5月4日', date: 1651593600000, note: '', }
3: {amount: '5427.00', category: '+', createdAt: '2022年5月1日', date: 1651334400000, note: '', }
4: {amount: '255.00', category: '-', createdAt: '2022年4月7日', date: 1649260800000, note: '', }
5: {amount: '2542.00', category: '+', createdAt: '2022年4月1日', date: 1648742400000, note: '', }
6: {amount: '788.00', category: '+', createdAt: '2022年4月1日', date: 1648742400000, note: '', }
7: {amount: '25.00', category: '-', createdAt: '2022年4月3日', date: 1648915200000, note: '', }
8: {amount: '254.00', category: '-', createdAt: '2022年4月3日', date: 1648915200000, note: '', }
9: {amount: '254.00', category: '-', createdAt: '2022年4月9日', date: 1649433600000, note: '', }
10: {amount: '2558.00', category: '+', createdAt: '2022年4月10日', date: 1649520000000, note: '', }
11: {amount: '2555.00', category: '+', createdAt: '2022年4月25日', date: 1650816000000, note: '', }
12: {amount: '254.00', category: '+', createdAt: '2022年5月3日', date: 1651568739662, note: '', }
13: {amount: '287.00', category: '-', createdAt: '2022年4月4日', date: 1649001600000, note: '', }
14: {amount: '87.00', category: '+', createdAt: '2022年4月4日', date: 1649001600000, note: '',}
]
变成
[
{
name:'2022年5月3日',
child:[
{amount: '12.00', category: '-', createdAt: '2022年5月3日', date: 1651567753661, note: '', }
]
},
]
把同一天创建的数据提取出来并插入新数组中
// 将数值转变
changeList(arr) {
let obj = {}
let treeList = [];
//循环数组
arr.forEach((item, index) => {
//创建日期作为对象名,并创建child数组
obj = {
name: item.createdAt,
child: []
};
treeList.push(obj)
})
//数组对象去重,将相同日期去重
let test = {}
treeList = treeList.reduce((preVal, curVal) => {
test[curVal.name] ? '' : test[curVal.name] = preVal.push(curVal)
return preVal
}, [])
//源数组继续循环,将相同日期的数据push到child数组中
arr.forEach((item, index) => {
treeList.forEach((data, i) => {
if (item.createdAt == data.name) {
treeList[i].child.push(item)
}
})
})
// console.log(this.treeList)
}
2.对象属性名的更改 利用JSON和replace 进行更改
changeList(arr){
arr.forEach(item => {
item = JSON.parse(JSON.stringify(item).replace(/amount/g, "value"))
});
}