Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何优化代码?_Javascript_Node.js_Algorithm_Sorting - Fatal编程技术网

Javascript 如何优化代码?

Javascript 如何优化代码?,javascript,node.js,algorithm,sorting,Javascript,Node.js,Algorithm,Sorting,我有一个对象数组: [{ dtype: 2,geoid: 1, hits: '1046149', uniq: '955755', pname: '95940_651577-2711871' }, { dtype: 3, geoid: 1, hits: '2

我有一个对象数组:

[{ 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 uniqtmsec=[];
var o_结果=[];
// Формирование списка уникальных таймсеков
对于(var i=0;i
所以有一种观点认为这不是一个很好的决定。。。 还有一种更短的方法可以使用Python中的字典之类的对象来实现这一点。 是这样吗? 如果没有,我如何改进我的决策并使其更快? 如果有一种方法可以通过迭代对象(字典)实现这一点,那么如何实现呢?

您可以使用一个,并将
tmsec
geoid
作为键

分组后,您只需要这些值

var data=[{dtype:2,大地水准面:1,点击率:'1046149',uniq:955755',pname:95940651577-2711871',{dtype:3,大地水准面:1,点击率:'2167',uniq:1846',pname:95940651577-2711871',{dtype:5,geoid:1,点击率:'32',uniq:31',pname:95940651577-2711871'},{dtype:1,大地水准面:2,点击率:'1031246',uniq:942156',pname:95940651577-2711871'},{dtype:2,大地水准面:2,点击率:'1029091',uniq:940319',pname:95940651577-2711871'},{dtype:3,大地水准面:2,点击率:'2123',uniq:1806',pname:95940651577-2711871'},{dtype:5,geoid:2,点击率:'32',uniq:31',PNU:1871'},{dtype:1,大地水准面:2,点击率:'1031246',uniq:942156',pname:95940651577-2711879',{dtype:2,大地水准面:2,点击率:'1029091',uniq:940319',pname:95940651577-2711879',{dtype:3,大地水准面:2,点击率:'2123',uniq:1806',pname:95940651577-2711877'},
结果=数组。从(数据)
.reduce((m,{pname:tmsec,geoid,dtype,hits,uniq})=>{
var key=[tmsec,geoid].join(“|”),
组=m.get(键);
如果(!组){
组={tmsec,大地水准面,数据:[]};
m、 设置(键、组);
}
push({dtype,hits,uniq});
返回m;
},新地图)
.values()
);
console.log(结果);

.as console wrapper{max height:100%!important;top:0;}
您想按
geoid
分组还是按
geoid
pname
按geoid和pname分组?您使用ES6吗?是的,我使用ES6。。
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;
                                }
                            }
                        }
                    }