Saturday, 13 May 2017

How to sort value and filter only last 7 day result on firebase

Here is data on firebase database

{
  "urls" : {
    "beauty-credit" : {
      "count" : 1,
      "keyword" : "beauty credit",
      "last_res" : 152,
      "last_sea" : "Sat May 13 2017 15:16:41 GMT+0700 (SE Asia Standard Time)",
      "url_site" : "beauty-credit"
    },
    "etude" : {
      "count" : 2,
      "keyword" : "etude",
      "last_res" : 1048,
      "last_sea" : "Sat May 13 2017 15:16:38 GMT+0700 (SE Asia Standard Time)",
      "url_site" : "etude"
    },
    "salmon" : {
      "count" : 1,
      "keyword" : "salmon",
      "last_res" : 25,
      "last_sea" : "Sat May 1 2017 15:16:47 GMT+0700 (SE Asia Standard Time)",
      "url_site" : "salmon"
    },
    "skinfood" : {
      "count" : 2,
      "keyword" : "skinfood",
      "last_res" : 478,
      "last_sea" : "Sat May 13 2017 15:16:45 GMT+0700 (SE Asia Standard Time)",
      "url_site" : "skinfood"
    }
  }
}

and I use this code to get data from firebase

function showTopsearch(req, res){
    database = firebase.database();
    var ref = database.ref('urls');
    ref.on('value', gotData, errData);

    function errData(err){
        console.log('Error!');
        console.log(err);
    }

    function gotData(data){
        result = data.val() 
        var urls = Object.keys(result)
                    .filter(key => result[key].last_res > 10)
        console.log(urls)
        }
    res.render('topsearch.html')
    }

Then I got this data

[ 'beauty-credit', 'etude', 'salmon', 'skinfood' ]

I need to add more rule on last_sea that filter only last 7 day, sort on count, and show only 3 row after sort data. How can I do that?



via Moomoo Soso

No comments:

Post a Comment