将对象转换为数组并将地图名称转换为键和子项

时间:2015-07-21 09:54:23

标签: javascript sorting lodash

我有这个数据集

var items = {
    1: {
        id: 1,
        name: 'foo',
        date: {
            MMM  : 'february',
            yyyy : '2014',
            S    : '14th'
        }
    },
    2: {
        id: 2,
        name: 'bar',
        date: {
            MMM  : 'february',
            yyyy : '2014',
            S    : '7th'
        }
    },
    3: {
        id: 3,
        name: 'bazz',
        date: {
            MMM  : 'august',
            yyyy : '2015',
            S    : '21st'
        }
    },
    4: {
        id: 4,
        name: 'sup',
        date: {
            MMM  : 'may',
            yyyy : '2015',
            S    : '8th'
        }
    },
    5: {
        id: 6,
        name: 'awe',
        date: {
            MMM  : 'february',
            yyyy : '2014',
            S    : '1st'
        }
    }

};

我使用lodash将它们与这个自定义函数分组:

_.groupByMulti = function(obj, values, context) {
    if (!values.length) {
        return obj;
    }
    var byFirst = _.chain(obj)
    .groupBy(values[0], context)
    .value();

    var rest = values.slice(1);

    for (var prop in byFirst) {
        if (prop) {
            byFirst[prop] = _.groupByMulti(byFirst[prop], rest, context);
        }
    }

    return byFirst;
}

所以_.groupByMulti(items, ['date.yyyy', 'date.MMM', 'date.S']);将重新开始:

{
    "2014": {
        "february": {
            "14th": [
                {
                    "id": 1,
                    "name": "foo",
                    "date": {
                        "MMM": "february",
                        "yyyy": "2014",
                        "S": "14th"
                    }
                }
            ],
            "7th": [
                {
                    "id": 2,
                    "name": "bar",
                    "date": {
                        "MMM": "february",
                        "yyyy": "2014",
                        "S": "7th"
                    }
                }
            ],
            "1st": [
                {
                    "id": 6,
                    "name": "awe",
                    "date": {
                        "MMM": "february",
                        "yyyy": "2014",
                        "S": "1st"
                    }
                }
            ]
        }
    },
    "2015": {
        "august": {
            "21st": [
                {
                    "id": 3,
                    "name": "bazz",
                    "date": {
                        "MMM": "august",
                        "yyyy": "2015",
                        "S": "21st"
                    }
                }
            ]
        },
        "may": {
            "8th": [
                {
                    "id": 4,
                    "name": "sup",
                    "date": {
                        "MMM": "may",
                        "yyyy": "2015",
                        "S": "8th"
                    }
                }
            ]
        }
    }
}

现在我如何扩展我的lodash函数,以便它将对象转换为数组,并让它们像这样:

[
    {
       key: '2015',
       children: [
           {
              key: 'february',
              children: [...]
           }
       ]
    }
]

2 个答案:

答案 0 :(得分:1)

鉴于你目前有:

var out = _.groupByMulti(items, ['date.yyyy', 'date.MMM', 'date.S']);

返回一个对象。要将它包装到数组中,这可能会起到作用:

_.map(_.keys(out), function(year){

    return {
        key: year,
        children: _.map(_.keys(out[year]),function(month){
            return {
                key: month,
                children: out[year][month]
            }
        })
    }
});

答案 1 :(得分:0)

@Tao P. R.的回答之后,我能够像这样扩展pppX函数:

groupByMulti