用哈巴狗对JSON对象进行排序

时间:2018-01-19 09:20:53

标签: node.js pug pugjs

目前我正在使用此代码对nodejs代码中的数据进行排序。

const popularPlaces = await Places
.find({location: "New York"})
.sort( { locationscore: -1 } )

上面的代码找到了地点New York,并将排名较高locationscore的热门地点排序并转储到popularPlaces。这样我可以在pug/jade模板上使用它,如下所示:

each value in popularPlaces
  .h2 Top Popular Places #{value.placename}

但我想在pug/jade本身进行排序......而不是由nodejs进行排序。

这是popularPlaces

返回的JSON
[{
    "placename": "Modern Stop", 
    "locationscore": 0.8734, 
},{
    "placename": "Next Coffee", 
    "locationscore": 0.807121, 
},{
    "placename": "Mahattan Bakery", 
    "locationscore": 0.899802, 
},{
    "placename": "Mainland Cafe", 
    "locationscore": 0.801271, 
},{
    "placename": "Combo Boo", 
    "locationscore": 0.808973, 
},{
    "placename": "Baker's Bakery", 
    "locationscore": 0.8123, 
}] 

我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试

- popularPlaces.sort(function(a,b){ return (a.locationscore > b.locationscore)?1:((b.locationscore > a.locationscore)?-1:0);})

each value in popularPlaces
  .h2 Top Popular Places #{value.placename}