格式化一长串js对象,将日期和数字,日期和颜色的关系与日期和数字,颜色和日期的关系分开 [英] Format a long array of js object separating dates to number and color relation to month to dates to numbers and respective colors

查看:78
本文介绍了格式化一长串js对象,将日期和数字,日期和颜色的关系与日期和数字,颜色和日期的关系分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let dates = {
              '2018/07/25': [['1','red'], ['1','orange'], ['3','blue']],
              '2018/07/26': [['2','black'], ['4','orange'], ['4','pink']],
              '2018/08/01': [['3','purple'], ['4','green']]
            } 

预期的newObject是:

The newObject expected is:

newObject = {
             '2018/07':{
                         '2018/07/25': {
                                         '1':['red','orange'],
                                         '3':['blue']
                                       },
                         '2018/07/26': {
                                         '2':['black'],
                                         '4':['orange','pink']
                                       }
                       },
             '2018/08':{
                         '2018/08/01': {
                                         '3':['purple'],
                                         '4':['green']
                                       }
                       }
             }

到目前为止,我的代码:

My code so far:

let newObject = {};

Object.keys(dates).forEach((onedate, index) => {
    let monthdate = onedate.slice(0,7)
    if (!newObject[monthdate]) {
        newObject[monthdate] = {};
    }    
    newObject[monthdate][onedate] = [...dates[onedate]];
});

以上代码的输出:

newObject = {
             "2018/07":{
                         "2018/07/25": [["1","red"],["1","orange"],["3","blue"]],
                         "2018/07/26": [["2","black"],["4","orange"],["4","pink"]]
                        },
             "2018/08":{
                         "2018/08/01": [["3","purple"],["4","green"]]
                       }
             }

我也想删除3级深度的数组. 也可以使用Set分隔第一个数字,并分别与颜色相关

I want to remove array in level 3 depth too. Also separate the first number maybe using Sets and have their respective relation to colors

"2018/07/25": [["1","red"],["1","orange"],["3","blue"]]

'2018/07/25': {'1':['red','orange'],'3':['blue']}

推荐答案

可以在数组上使用基本的reduce()将它们映射到所需的对象结构

Can use a basic reduce() on the arrays to map them to object structure you want

let dates = {
              '2018/07/25': [['1','red'], ['1','orange'], ['3','blue']],
              '2018/07/26': [['2','black'], ['4','orange'], ['4','pink']],
              '2018/08/01': [['3','purple'], ['4','green']]
            }
            
let newObject = {};

Object.keys(dates).forEach((onedate, index) => {
    let monthdate = onedate.slice(0,7)
    if (!newObject[monthdate]) {
        newObject[monthdate] = {};
    }    
    newObject[monthdate][onedate] = dates[onedate].reduce((a,c)=>{
        a[c[0]] = a[c[0]] || [];
        a[c[0]].push(c[1])
        return a;
    },{})
});

console.log(newObject)

这篇关于格式化一长串js对象,将日期和数字,日期和颜色的关系与日期和数字,颜色和日期的关系分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆