如何优化代码?

时间:2018-06-16 06:33:00

标签: javascript node.js algorithm sorting

我有一个对象数组:

[{ dtype: 2,geoid: 1,
                     hits: '1046149',
                     uniq: '955755',
                     pname: '95940_651577-2711871' },
                 { dtype: 3,
                     geoid: 1,
                     hits: '2167',
                     uniq: '1846',
                     pname: '95940_651577-2711871' },
                 { dtype: 5,
                     geoid: 1,
                     hits: '32',
                     uniq: '31',
                     pname: '95940_651577-2711871' },
                 { dtype: 1,
                     geoid: 2,
                     hits: '1031246',
                     uniq: '942156',
                     pname: '95940_651577-2711871' },
                 { dtype: 2,
                     geoid: 2,
                     hits: '1029091',
                     uniq: '940319',
                     pname: '95940_651577-2711871' },
                 { dtype: 3,
                     geoid: 2,
                     hits: '2123',
                     uniq: '1806',
                     pname: '95940_651577-2711871' },
                 { dtype: 5,
                     geoid: 2,
                     hits: '32',
                     uniq: '31',
                     pname: '95940_651577-2711871' }]

我需要制作一个像这样的新数组:

   [{"tmsec":"95940_651577-2711858",
           "geoid":"1",
           "data":
           [{"dtype":"1", "hits":"1486931", "uniq":"875488"},
            {"dtype":"2", "hits":"1375478", "uniq":"797820"},
            {"dtype":"3", "hits":"104913", "uniq":"73517"},
            {"dtype":"4", "hits":"6540", "uniq":"4164"}]
           },   {"tmsec":"95940_651577-2711858",    "geoid":"2",    "data":
           [{"dtype":"1", "hits":"1486931", "uniq":"875488"},
            {"dtype":"2", "hits":"1375478", "uniq":"797820"},
            {"dtype":"3", "hits":"104913", "uniq":"73517"},
            {"dtype":"4", "hits":"6540", "uniq":"4164"}]
           },   {"tmsec":"95940_651577-2711858",    "geoid":"1",    "data":
           [{"dtype":"1", "hits":"1486931", "uniq":"875488"},
            {"dtype":"2", "hits":"1375478", "uniq":"797820"},
            {"dtype":"3", "hits":"104913", "uniq":"73517"},
            {"dtype":"4", "hits":"6540", "uniq":"4164"}]
           },  ]

我用两个“for”做了这个:

var uniqTmsecs = [];
                var o_result = [];
                // Формирование списка уникальных таймсеков
                for (var i = 0; i < result.rows.length; i++){
                     uniqTmsecs.push(result.rows[i].pname);
                }
                console.log(result.rows);
                var uSet = new Set(uniqTmsecs);
                uniqTmsecs = [...uSet];
                // Добавление в список объектов с уникальным таймсеком, геопозицией и типами устройств
                for (var i = 0; i < uniqTmsecs.length; i++){
                    for (var j = 1; j < 3; j++) {
                        var obj = {
                            tmsec: uniqTmsecs[i], geoid: j, data:
                                [{dtype: 1, hits: 0, uniq: 0},
                                    {dtype: 2, hits: 0, uniq: 0},
                                    {dtype: 3, hits: 0, uniq: 0},
                                    {dtype: 5, hits: 0, uniq: 0}]
                        };
                        o_result.push(obj);
                    }
                }
                // Добавление данных к каждому объекту
                for (var i = 0; i < o_result.length; i++){
                    var tmsec = o_result[i].tmsec;
                    var geoid = o_result[i].geoid;
                    for (var j = 0; j < result.rows.length; j++) {
                        if(tmsec===result.rows[j].pname && 
                           geoid===result.rows[j].geoid){
                            for (var x = 0; x < o_result[i].data.length; x++) {                                    if (o_result[i].data[x].dtype===result.rows[j].dtype){
                                    o_result[i].data[x].hits = result.rows[j].hits;
                                    o_result[i].data[x].uniq = result.rows[j].uniq;
                                }
                            }
                        }
                    }

所以有一个想法,这不是一个非常“好”的决定...... 使用Python中的字典之类的对象有一种更短的方法。 是这样吗? 如果不是,我怎样才能改进决策并使其更快? 如果有办法用迭代对象(字典)如何做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用Map并以tmsecgeoid为关键字。

分组后,您只需要值。

var data = [{ dtype: 2, geoid: 1, hits: '1046149', uniq: '955755', pname: '95940_651577-2711871' }, { dtype: 3, geoid: 1, hits: '2167', uniq: '1846', pname: '95940_651577-2711871' }, { dtype: 5, geoid: 1, hits: '32', uniq: '31', pname: '95940_651577-2711871' }, { dtype: 1, geoid: 2, hits: '1031246', uniq: '942156', pname: '95940_651577-2711871' }, { dtype: 2, geoid: 2, hits: '1029091', uniq: '940319', pname: '95940_651577-2711871' }, { dtype: 3, geoid: 2, hits: '2123', uniq: '1806', pname: '95940_651577-2711871' }, { dtype: 5, geoid: 2, hits: '32', uniq: '31', pname: '95940_651577-2711871' }, { dtype: 1, geoid: 2, hits: '1031246', uniq: '942156', pname: '95940_651577-2711879' }, { dtype: 2, geoid: 2, hits: '1029091', uniq: '940319', pname: '95940_651577-2711879' }, { dtype: 3, geoid: 2, hits: '2123', uniq: '1806', pname: '95940_651577-2711877' }],
    result = Array.from(data
        .reduce((m, { pname: tmsec, geoid, dtype, hits, uniq }) => {
            var key = [tmsec, geoid].join('|'),
                group = m.get(key);

            if (!group) {
                group = { tmsec, geoid, data: [] };
                m.set(key, group);
            }
            group.data.push({ dtype, hits, uniq });
            return m;
        }, new Map)
       .values()
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }