使用NODEJS对JSON文件进行排序

时间:2017-05-28 10:05:21

标签: arrays json node.js quicksort

我刚刚开始使用NodeJS(截至昨天)我已经设法从API中检索JSON数据,如下面的代码所示:

var request = require('request');
var fs = require('fs');
const pug = require('pug');

request.get('APICALL', function(err, data) {
if (err) { }
else {



console.log(data);

fs.writeFile('industries.txt', JSON.stringify(data));

}
});

我的request.get返回的JSON数据可以在JSON FILE

找到

我想要做的是对这些数据进行分类,找出哪个收入和行业ID最多(例如,这个数据是“收入:8”和“行业:69”)一旦完成,我想要返回只有那些同时兼顾收入8和行业9的公司。

这是可能的吗?什么是接近这项任务的最佳方法?

1 个答案:

答案 0 :(得分:1)

您可以计算收入 - industry_id出现次数,按降序排序,然后按此过滤:



let arr = [{"id":1,"domain":"google.com","alexa_rank":1,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":3,"domain":"youtube.com","alexa_rank":2,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":2,"domain":"facebook.com","alexa_rank":3,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":4,"domain":"yahoo.com","alexa_rank":6,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":111,"domain":"reddit.com","alexa_rank":9,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":9,"domain":"twitter.com","alexa_rank":12,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":230,"domain":"360.cn","alexa_rank":20,"country":46,"employees":"5","revenue":"8","industry_id":"69"},{"id":30,"domain":"weibo.com","alexa_rank":22,"country":46,"employees":"5","revenue":"2","industry_id":"69"},{"id":19,"domain":"google.de","alexa_rank":23,"country":236,"employees":"7","revenue":"8","industry_id":"65"},{"id":23,"domain":"google.co.uk","alexa_rank":24,"country":236,"employees":"7","revenue":"8","industry_id":"45"},{"id":12,"domain":"linkedin.com","alexa_rank":25,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":25,"domain":"google.fr","alexa_rank":26,"country":76,"employees":"1","revenue":"2","industry_id":"65"},{"id":46,"domain":"google.ru","alexa_rank":27,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":33,"domain":"google.com.br","alexa_rank":28,"country":32,"employees":"3","revenue":"3","industry_id":"47"},{"id":22,"domain":"yandex.ru","alexa_rank":29,"country":182,"employees":"5","revenue":"7","industry_id":"69"},{"id":20,"domain":"google.com.hk","alexa_rank":31,"country":46,"employees":"0","revenue":"8","industry_id":"69"},{"id":101,"domain":"netflix.com","alexa_rank":32,"country":236,"employees":"5","revenue":"8","industry_id":"38"},{"id":35,"domain":"google.it","alexa_rank":34,"country":236,"employees":"6","revenue":"8","industry_id":"113"},{"id":3351716,"domain":"ntd.tv","alexa_rank":36,"country":236,"employees":"4","revenue":"0","industry_id":"13"},{"id":77,"domain":"imgur.com","alexa_rank":37,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":21,"domain":"ebay.com","alexa_rank":38,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":41,"domain":"google.es","alexa_rank":39,"country":236,"employees":"1","revenue":"2","industry_id":"65"},{"id":74,"domain":"pornhub.com","alexa_rank":40,"country":58,"employees":"1","revenue":"3","industry_id":"38"},{"id":13,"domain":"msn.com","alexa_rank":41,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":18,"domain":"wordpress.com","alexa_rank":43,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":507,"domain":"aliexpress.com","alexa_rank":44,"country":46,"employees":"7","revenue":"8","industry_id":"69"},{"id":42,"domain":"livejasmin.com","alexa_rank":47,"country":128,"employees":"1","revenue":"0","industry_id":"38"},{"id":28,"domain":"microsoft.com","alexa_rank":48,"country":236,"employees":"8","revenue":"8","industry_id":"25"},{"id":58,"domain":"google.ca","alexa_rank":49,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":44,"domain":"tumblr.com","alexa_rank":50,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":107,"domain":"stackoverflow.com","alexa_rank":52,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":2553,"domain":"twitch.tv","alexa_rank":53,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":40,"domain":"imdb.com","alexa_rank":58,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":488,"domain":"github.com","alexa_rank":61,"country":236,"employees":"4","revenue":"5","industry_id":"25"},{"id":239,"domain":"pinterest.com","alexa_rank":62,"country":236,"employees":"4","revenue":"4","industry_id":"69"},{"id":1853,"domain":"popads.net","alexa_rank":65,"country":53,"employees":"1","revenue":"1","industry_id":"84"},{"id":51,"domain":"xvideos.com","alexa_rank":66,"country":59,"employees":"0","revenue":"0","industry_id":"38"},{"id":7154677,"domain":"diply.com","alexa_rank":68,"country":40,"employees":"3","revenue":"0","industry_id":"100"},{"id":245,"domain":"csdn.net","alexa_rank":69,"country":46,"employees":"3","revenue":"2","industry_id":"69"},{"id":189,"domain":"wikia.com","alexa_rank":72,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":275,"domain":"google.com.tw","alexa_rank":74,"country":218,"employees":"0","revenue":"8","industry_id":"95"},{"id":8456,"domain":"whatsapp.com","alexa_rank":75,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":56,"domain":"xhamster.com","alexa_rank":78,"country":58,"employees":"1","revenue":"0","industry_id":"38"},{"id":44705842,"domain":"txxx.com","alexa_rank":81,"country":0,"employees":"1","revenue":"0","industry_id":"38"},{"id":2793171,"domain":"coccoc.com","alexa_rank":84,"country":243,"employees":"3","revenue":"0","industry_id":"65"},{"id":1000482,"domain":"bongacams.com","alexa_rank":85,"country":58,"employees":"0","revenue":"0","industry_id":"38"},{"id":92,"domain":"google.pl","alexa_rank":86,"country":236,"employees":"6","revenue":"8","industry_id":"77"},{"id":794,"domain":"pixnet.net","alexa_rank":86,"country":218,"employees":"3","revenue":"0","industry_id":"69"},{"id":249,"domain":"dropbox.com","alexa_rank":87,"country":236,"employees":"5","revenue":"7","industry_id":"69"},{"id":62718286,"domain":"porn555.com","alexa_rank":90,"country":0,"employees":"0","revenue":"0","industry_id":"38"},{"id":141,"domain":"google.co.th","alexa_rank":91,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":1215,"domain":"gmw.cn","alexa_rank":92,"country":46,"employees":"0","revenue":"0","industry_id":"97"},{"id":130,"domain":"google.com.pk","alexa_rank":93,"country":236,"employees":"3","revenue":"2","industry_id":"84"},{"id":317,"domain":"china.com","alexa_rank":98,"country":46,"employees":"5","revenue":"8","industry_id":"100"},{"id":148727,"domain":"amazon.in","alexa_rank":100,"country":236,"employees":"8","revenue":"8","industry_id":"69"},{"id":115,"domain":"google.com.ar","alexa_rank":101,"country":11,"employees":"0","revenue":"8","industry_id":"69"},{"id":375,"domain":"soundcloud.com","alexa_rank":103,"country":83,"employees":"4","revenue":"3","industry_id":"69"},{"id":39,"domain":"fc2.com","alexa_rank":104,"country":236,"employees":"0","revenue":"0","industry_id":"69"},{"id":62729524,"domain":"clicksgear.com","alexa_rank":104,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":2711,"domain":"bbc.com","alexa_rank":107,"country":235,"employees":"6","revenue":"8","industry_id":"13"},{"id":109,"domain":"google.nl","alexa_rank":108,"country":155,"employees":"7","revenue":"8","industry_id":"65"},{"id":146,"domain":"adf.ly","alexa_rank":110,"country":235,"employees":"1","revenue":"2","industry_id":"65"},{"id":7739,"domain":"uptodown.com","alexa_rank":111,"country":209,"employees":"1","revenue":"2","industry_id":"69"},{"id":26665,"domain":"ettoday.net","alexa_rank":111,"country":46,"employees":"0","revenue":"0","industry_id":"100"},{"id":240,"domain":"booking.com","alexa_rank":112,"country":155,"employees":"6","revenue":"8","industry_id":"69"},{"id":95,"domain":"dailymotion.com","alexa_rank":113,"country":76,"employees":"4","revenue":"5","industry_id":"69"},{"id":1022,"domain":"quora.com","alexa_rank":115,"country":236,"employees":"3","revenue":"2","industry_id":"69"},{"id":48,"domain":"ask.com","alexa_rank":116,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":2353727,"domain":"espn.com","alexa_rank":117,"country":236,"employees":"5","revenue":"7","industry_id":"13"},{"id":7170357,"domain":"blastingnews.com","alexa_rank":119,"country":216,"employees":"4","revenue":"2","industry_id":"100"},{"id":86,"domain":"nytimes.com","alexa_rank":120,"country":236,"employees":"5","revenue":"8","industry_id":"97"},{"id":1000001,"domain":"blogger.com","alexa_rank":124,"country":236,"employees":"7","revenue":"8","industry_id":"69"},{"id":106,"domain":"vimeo.com","alexa_rank":125,"country":236,"employees":"4","revenue":"5","industry_id":"69"},{"id":10725,"domain":"savefrom.net","alexa_rank":125,"country":182,"employees":"1","revenue":"0","industry_id":"69"},{"id":28040741,"domain":"daikynguyenvn.com","alexa_rank":126,"country":236,"employees":"0","revenue":"0","industry_id":"100"},{"id":547,"domain":"detik.com","alexa_rank":127,"country":103,"employees":"4","revenue":"5","industry_id":"100"},{"id":1282,"domain":"stackexchange.com","alexa_rank":129,"country":236,"employees":"4","revenue":"3","industry_id":"69"},{"id":200,"domain":"google.co.ve","alexa_rank":131,"country":236,"employees":"0","revenue":"8","industry_id":"69"},{"id":157,"domain":"google.co.za","alexa_rank":132,"country":205,"employees":"2","revenue":"2","industry_id":"82"},{"id":221,"domain":"salesforce.com","alexa_rank":132,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":2394,"domain":"vice.com","alexa_rank":135,"country":236,"employees":"5","revenue":"8","industry_id":"118"},{"id":4637,"domain":"tribunnews.com","alexa_rank":136,"country":103,"employees":"3","revenue":"2","industry_id":"87"},{"id":88,"domain":"ebay.co.uk","alexa_rank":138,"country":236,"employees":"6","revenue":"8","industry_id":"69"},{"id":177,"domain":"slideshare.net","alexa_rank":140,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":1816926,"domain":"theguardian.com","alexa_rank":141,"country":235,"employees":"5","revenue":"6","industry_id":"97"},{"id":1594,"domain":"spotify.com","alexa_rank":143,"country":215,"employees":"5","revenue":"8","industry_id":"95"},{"id":113,"domain":"xnxx.com","alexa_rank":144,"country":59,"employees":"1","revenue":"0","industry_id":"38"},{"id":236,"domain":"google.com.vn","alexa_rank":145,"country":243,"employees":"0","revenue":"8","industry_id":"9"},{"id":44263656,"domain":"adexchangeprediction.com","alexa_rank":146,"country":0,"employees":"0","revenue":"0","industry_id":"84"},{"id":884,"domain":"buzzfeed.com","alexa_rank":147,"country":236,"employees":"5","revenue":"5","industry_id":"69"},{"id":4263,"domain":"chaturbate.com","alexa_rank":148,"country":236,"employees":"2","revenue":"1","industry_id":"38"},{"id":161,"domain":"nicovideo.jp","alexa_rank":149,"country":111,"employees":"4","revenue":"5","industry_id":"69"},{"id":225,"domain":"google.gr","alexa_rank":149,"country":86,"employees":"1","revenue":"1","industry_id":"35"},{"id":158,"domain":"chase.com","alexa_rank":151,"country":236,"employees":"8","revenue":"8","industry_id":"44"},{"id":148,"domain":"mozilla.org","alexa_rank":153,"country":236,"employees":"5","revenue":"6","industry_id":"69"},{"id":723,"domain":"avito.ru","alexa_rank":153,"country":182,"employees":"4","revenue":"4","industry_id":"69"},{"id":87,"domain":"cnet.com","alexa_rank":155,"country":236,"employees":"4","revenue":"5","industry_id":"100"},{"id":224,"domain":"google.com.co","alexa_rank":156,"country":49,"employees":"7","revenue":"2","industry_id":"47"},{"id":340,"domain":"indeed.com","alexa_rank":157,"country":236,"employees":"5","revenue":"8","industry_id":"69"},{"id":558,"domain":"flipkart.com","alexa_rank":157,"country":102,"employees":"6","revenue":"8","industry_id":"69"}]

let mostOccurence = [...arr.reduce((a,b) => {
    a.set(b.revenue + ":" + b.industry_id, a.has(b.revenue + ":" + b.industry_id) ? a.get(b.revenue + ":" + b.industry_id) + 1 : 1);
    return a;
}, new Map)].sort((a,b) => b[1] - a[1])[0][0];

let split = mostOccurence.split(':'), revToLook = split[0], indToLook = split[1];
let filtered = arr.filter(e => e.revenue === revToLook && e.industry_id === indToLook);
console.log(filtered);