javascript - Filtering of result according the max and min -
i have function in javascript compare 2 values of 2 turnover of 2 results of sql queries.
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]; }); }); // report on data var output = []; (client in data) { (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);
when change function, affecting result a variable sort after, variable turnovers
becames not recognized.
like this:
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 )); }
how can resolve problem please ?
this example of result of function:
"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"
you sort data percent value , take top 10 results or bottom 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; }
Comments
Post a Comment