根据最大值和最小值过滤结果

时间:2017-08-18 13:21:09

标签: javascript

我在JavaScript中有一个函数,可以比较2个SQL查询结果的2个周转值。

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
                  + " " + (percent = turnovers[0] === 0 ?
                               turnovers[1] : 
                              ((turnovers[1]-turnovers[0]) /turnovers[0])*100

        ));

}

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

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

当我更改我的函数时,通过影响结果变量来对其进行排序,我的变量turnovers无法识别。

像这样:

function formatName(client,article,turnover,a) {

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

        ));

}

我该如何解决这个问题?

这是此功能的结果示例:

"Client: 347, LRU.: ECP, semester t/o: 1026,-2510 -344.63937621832355"
"Client: 347, LRU.: NSS, semester t/o: 6259,0 -100"
"Client: 394, LRU.: ATSU, semester t/o: 16951,16951 0"
"Client: 394, LRU.: FCPC, semester t/o: 3178,3315 4.310887350534927"
"Client: 394, LRU.: FCSC, semester t/o: 2536,0 -100"
"Client: 394, LRU.: SDAC, semester t/o: 7391,0 -100"
"Client: 407, LRU.: RADOME, semester t/o: 4611,-927 -120.10409889394926"
"Client: 417, LRU.: FWC, semester t/o: 0,3965 3965"
"Client: 417, LRU.: SDAC, semester t/o: 601,601 0"
"Client: 446, LRU.: MFC, semester t/o: 4232,4063 -3.993383742911153"
"Client: 450, LRU.: ATSU, semester t/o: 892,0 -100"
"Client: 450, LRU.: FWC, semester t/o: 918,0 -100"
"Client: 467, LRU.: ATSU, semester t/o: 887,0 -100"
"Client: 467, LRU.: ECP, semester t/o: 2500,2616 4.64"
"Client: 467, LRU.: FCPC, semester t/o: 3918,3539 -9.673302705461971"
"Client: 467, LRU.: FCSC, semester t/o: 5254,7564 43.96650171298059"
"Client: 467, LRU.: FWC, semester t/o: 2592,2089 -19.405864197530864"
"Client: 467, LRU.: RADOME, semester t/o: 7404,16815 127.10696920583469"
"Client: 467, LRU.: SDAC, semester t/o: 1310,1108 -15.419847328244273"
"Client: 512, LRU.: MFC, semester t/o: 2785,2128 -23.590664272890486"
"Client: 540, LRU.: ATSU, semester t/o: 25534,0 -100"
"Client: 554, LRU.: ECP, semester t/o: 750,750 0"
"Client: 554, LRU.: FWC, semester t/o: 954,1161 21.69811320754717"
"Client: 554, LRU.: SDAC, semester t/o: 3848,3848 0"
"Client: 554, LRU.: FSA RCI, semester t/o: 0,10826 10826"
"Client: 554, LRU.: SEC, semester t/o: 0,901 901"
"Client: 554, LRU.: Unspecified, semester t/o: 0,428 428"
"Client: 594, LRU.: ATSU, semester t/o: 2059,2402 16.658572122389508"
"Client: 594, LRU.: ECP, semester t/o: 1957,1931 -1.3285641287685233"
"Client: 594, LRU.: FCPC, semester t/o: 4281,3174 -25.858444288717592"
"Client: 594, LRU.: FCSC, semester t/o: 2870,2552 -11.0801393728223"
"Client: 594, LRU.: FSA RCI, semester t/o: 3081,4477 45.309964297306074"
"Client: 594, LRU.: FWC, semester t/o: 5795,6959 20.0862812769629"
"Client: 594, LRU.: IMA EYY, semester t/o: 6348,0 -100"

1 个答案:

答案 0 :(得分:1)

您可以按百分比值对数据进行排序,并仅采用前10个结果或最低结果10。



function getPercent(array) {
    return array[0] === 0
        ? array[1]
        : (array[1] - array[0]) * 100 / array[0];
}

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

var data = [{ client: 347, article: 'ECP', turnovers: [1026, -2510] }, { client: 347, article: 'NSS', turnovers: [6259, 0] }, { client: 394, article: 'ATSU', turnovers: [16951, 16951] }, { client: 394, article: 'FCPC', turnovers: [3178, 3315] }, { client: 394, article: 'FCSC', turnovers: [2536, 0] }, { client: 394, article: 'SDAC', turnovers: [7391, 0] }, { client: 407, article: 'RADOME', turnovers: [4611, -927] }, { client: 417, article: 'FWC', turnovers: [0, 3965] }, { client: 417, article: 'SDAC', turnovers: [601, 601] }, { client: 446, article: 'MFC', turnovers: [4232, 4063] }, { client: 450, article: 'ATSU', turnovers: [892, 0] }, { client: 450, article: 'FWC', turnovers: [918, 0] }, { client: 467, article: 'ATSU', turnovers: [887, 0] }, { client: 467, article: 'ECP', turnovers: [2500, 2616] }, { client: 467, article: 'FCPC', turnovers: [3918, 3539] }, { client: 467, article: 'FCSC', turnovers: [5254, 7564] }, { client: 467, article: 'FWC', turnovers: [2592, 2089] }, { client: 467, article: 'RADOME', turnovers: [7404, 16815] }, { client: 467, article: 'SDAC', turnovers: [1310, 1108] }, { client: 512, article: 'MFC', turnovers: [2785, 2128] }, { client: 540, article: 'ATSU', turnovers: [25534, 0] }, { client: 554, article: 'ECP', turnovers: [750, 750] }, { client: 554, article: 'FWC', turnovers: [954, 1161] }, { client: 554, article: 'SDAC', turnovers: [3848, 3848] }, { client: 554, article: 'FSA RCI', turnovers: [0, 10826] }, { client: 554, article: 'SEC', turnovers: [0, 901] }, { client: 554, article: 'Unspecified', turnovers: [0, 428] }, { client: 594, article: 'ATSU', turnovers: [2059, 2402] }, { client: 594, article: 'ECP', turnovers: [1957, 1931] }, { client: 594, article: 'FCPC', turnovers: [4281, 3174] }, { client: 594, article: 'FCSC', turnovers: [2870, 2552] }, { client: 594, article: 'FSA RCI', turnovers: [3081, 4477] }, { client: 594, article: 'FWC', turnovers: [5795, 6959] }, { client: 594, article: 'IMA EYY', turnovers: [6348, 0] }];

data.forEach(function (o) {
    o.percent = getPercent(o.turnovers);
});

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

console.log('top', data.slice(0, 10));
console.log('bottom', data.slice(-10));    
console.log(data.map(function (o) {
    return formatName(o.client, o.article, o.turnovers);
}));
console.log(data);    

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