在JavaScript中排序结果

时间:2017-08-22 07:11:21

标签: javascript

我有2个sql查询来计算每个学期每个学期的营业额。

查询1的结果示例:

{
  "LRU": [
    "RADOME",
    "RADOME",
    "ATSU",
    "MFC",
    "FWC",
    "Unspecified",
    "AZE",
    "ECP",
    "CMM",
    "ECP"
  ],
  "Client": [
    17346,
    17512,
    7262,
    17242,
    4001,
    17164,
    7277,
    17334,
    8059,
    300015
  ],
  "round": [
    -33250,
    -13358,
    -11731,
    -10506,
    -6005,
    -3132,
    -2448,
    -2369,
    -2236,
    -2074
  ]
}

查询2的结果示例:

{
  "LRU": [
    "RADOME",
    "RADOME",
    "ECP",
    "PRIM",
    "MFC",
    "FWC",
    "RCI",
    "TAV",
    "CAL",
    "ECP"
  ],
  "Client": [
    17223,
    17346,
    7262,
    7956,
    594,
    4001,
    7277,
    17260,
    347,
    8059
  ],
  "round": [
    -34276,
    -33250,
    -11731,
    -6273,
    -5684,
    -4200,
    -2723,
    -2586,
    -2510,
    -2236
  ]
}

我做了一个javascript function来计算两个查询之间的差异(average),然后对它们进行排序,只采取前10个增加营业额的客户和前10个客户营业额减少。
这是我的功能:

    var query1 = {{repair_semestre1}};
var query2 = {{repair_semestre2}};
var data = {};
[query1, query2].forEach(function (query, semester) {
    query.Client.forEach(function(clientId, index) {
        var client = data[clientId] = data[clientId] || {};
        var clientArt = client[query.LRU[index]] = client[query.LRU[index]] || [0, 0];
        clientArt[semester] = query.round[index];
    });
});

// Now report on that data
var output = [];

for (client in data) {
    for (article in data[client]) {
        var turnovers = data[client][article];
            output.push(formatName(client,article,turnovers));
        }
}

          return {
             output: output 
            };
function formatName(client,article, turnover, a) {

    return("Client: " + client + ", LRU.: " + article 
                  + ", semester t/o: " + turnovers
                  + " " + (turnovers[0] === 0 ?
                                       turnovers[1] : 
                              ((turnovers[1]-turnovers[0]) /turnovers[0])*100

        ));

}

percent.sort(function (a, b) {
    return b.percent - a.percent;
});

return("top", percent.slice(0, 10));
return("bottom", percent.slice(-10));    
return(output);

这是她的结果(结果样本):

  {
  "output": [
    "Client: 347, LRU.: ECP, semester t/o: 0,-2510 -2510",
    "Client: 394, LRU.: ATSU, semester t/o: 0,10433 10433",
    "Client: 394, LRU.: FCPC, semester t/o: 0,3023 3023",
    "Client: 417, LRU.: FWC, semester t/o: 0,17683 17683",
    "Client: 417, LRU.: SDAC, semester t/o: 0,601 601",
    "Client: 446, LRU.: MFC, semester t/o: 0,3994 3994",
    "Client: 467, LRU.: ECP, semester t/o: 0,2595 2595",
    "Client: 467, LRU.: FCPC, semester t/o: 0,3687 3687",
    "Client: 467, LRU.: FCSC, semester t/o: 0,6731 6731",
    "Client: 467, LRU.: FWC, semester t/o: 0,1891 1891",
    "Client: 467, LRU.: RADOME, semester t/o: 0,16815 16815",
    "Client: 467, LRU.: SDAC, semester t/o: 0,1355 1355",
    "Client: 512, LRU.: MFC, semester t/o: 0,1523 1523",
    "Client: 554, LRU.: ECP, semester t/o: 0,750 750",
    "Client: 554, LRU.: FSA RCI, semester t/o: 0,10826 10826",
    "Client: 554, LRU.: FWC, semester t/o: 0,1161 1161",
    "Client: 554, LRU.: SDAC, semester t/o: 0,3848 3848",
    "Client: 554, LRU.: SEC, semester t/o: 0,428 428",
    "Client: 554, LRU.: Unspecified, semester t/o: 0,428 428"
   ]
}

请您更正我的功能,以查找我的客户的最大和最小值,该客户在LRU的营业额上增加/减少。 谢谢。

2 个答案:

答案 0 :(得分:1)

您的功能格式名称包含 4 参数,如您所见:

Phrase phrase = new Phrase("line1", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase, 40, 30, 0);
Phrase phrase2 = new Phrase("line2", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase2, 40, 0, 0);

你用 3

来称呼它
function formatName(client, article, turnover, a) 

而你正在尝试使用第四种,这显然无法奏效。您可以选择参数,然后测试它是否已设置,或者每次调用时都必须将其传递给函数。

编辑: 好的,我现在看到一些错误,我不确定你以前在做什么。

  • 数据变量被定义为对象而不是数组。因此,您不能将变量推入其中,并且不允许您对其进行迭代。
  • 在你的第一个循环中你没有做任何事情。实际上,您正在创建变量,但不会将它们推入数据阵列。使用output.push(formatName(client, article, turnovers));

执行此操作时:data.push(key, value)您只需使用变量var client = data[clientId] = data[clientId] || {};的值创建变量client(我提醒您为空)。

  • 然后你没有排序功能,所以你可以有一个排序输出。你的函数叫做sort,只做减法。
  • 最少但不是最后,你的语法,这很有趣。你没有退出功能。

以下是该代码中的主要错误。尝试纠正,然后会看到发生了什么。

答案 1 :(得分:1)

此提案基于给定的对象,并尝试通过使用具有所需属性的对象并按百分比对其进行排序来获得所需结果。

然后,为了获取顶部( max )10%的值,它会对数组进行切片并返回一个新数组,然后对该数组进行处理以获取所需的字符串及其中的信息。这同样适用于底部( min )百分比值。

function formatName(o) {
    return "Client: " + o.client + ", LRU.: " + o.article + ", semester t/o: " + o.turnovers + " " + o.percent;
}

var query1 = {
        LRU: ["RADOME", "RADOME", "Unspecified", "Unspecified", "Unspecified", "Unspecified", "Unspecified", "ECP", "ECP", "ECP"],
        Client: [17346, 17512, 7262, 17242, 4001, 17164, 7277, 17334, 8059, 300015],
        round: [-33250, -13358, -11731, -10506, -6005, -3132, -2448, -2369, -2236, -2074]
    },
    query2 = {
        LRU: ["RADOME", "RADOME", "Unspecified", "PRIM", "Unspecified", "Unspecified", "Unspecified", "ECP", "ECP", "ECP"],
        Client: [17223, 17346, 7262, 7956, 594, 4001, 7277, 17260, 347, 8059],
        round: [-34276, -33250, -11731, -6273, -5684, -4200, -2723, -2586, -2510, -2236]
    },
    data = {},
    output = [],
    max,
    min,
    client,
    article,
    turnovers;

[query1, query2].forEach(function (query, semester) {
    query.Client.forEach(function (clientId, index) {
        var client = data[clientId] = data[clientId] || {};
        var clientArt = client[query.LRU[index]] = client[query.LRU[index]] || [0, 0];
        clientArt[semester] = query.round[index];
    });
});

for (client in data) {
    for (article in data[client]) {
        turnovers = data[client][article];
        output.push({ client, article, turnovers, percent: turnovers[0] === 0 ? turnovers[1] : (turnovers[1] - turnovers[0]) * 100 / turnovers[0] });
    }
}

output.sort(function (a, b) {
    return b.percent - a.percent;
});

max = output.slice(0, 10); // get top 10
min = output.slice(-10);   // get bottom 10

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