【问题标题】:Sort JSON object with pug使用 pug 对 JSON 对象进行排序
【发布时间】:2018-01-19 09:20:52
【问题描述】:

目前我正在使用此代码对 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, 
}] 

我该怎么做?

【问题讨论】:

  • 请添加您的数据库提供的 JSON
  • @SandeepRanjan 我添加了返回的示例 JSON 数据
  • 你自己回答累了吗?
  • 我的解决方案有效吗?只是为了好,如果答案有效,您必须接受。

标签: node.js pug pugjs


【解决方案1】:

试试

- 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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 2018-02-13
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2021-04-03
    相关资源
    最近更新 更多