Javascript 如何将json文件连接到nodejs进行排序

Javascript 如何将json文件连接到nodejs进行排序,javascript,node.js,json,ejs,Javascript,Node.js,Json,Ejs,我从来没有在nodejs和ejs中使用过before-json,所以请原谅我的知识不足。我能够从json生成一个项目列表,但是当我按下下拉菜单进行排序时,除了根据下面的EJ按id进行默认排序外,它不会显示任何输出。我是否错误地连接了json nodejs router.get('/product', (req,res) =>{ fs.readFile('items.json', function (error, data) { if (error) {

我从来没有在nodejs和ejs中使用过before-json,所以请原谅我的知识不足。我能够从json生成一个项目列表,但是当我按下下拉菜单进行排序时,除了根据下面的EJ按id进行默认排序外,它不会显示任何输出。我是否错误地连接了json

nodejs

router.get('/product', (req,res) =>{
    fs.readFile('items.json', function (error, data) {
            if (error) {
                res.status(500).end()
            } else {
                res.render('product.ejs', {
                    user: req.user,
                    stripePublicKey: stripePublicKey,
                    items: JSON.parse(data)
                })
            }
    })
})

ejs

items.json

{
    "medicine": [
      {
        "id": 1,
        "name": "Bioderma",
        "price": 5000,
        "imgName": "product_01.png"
      },
      {
        "id": 2,
        "name": "Chanca Piedra",
        "price": 2300,
        "imgName": "product_02.png"
      },
      {
        "id": 3,
        "name": "Umcka",
        "price": 4000,
        "imgName": "product_03.png"
      },
      {
        "id": 4,
        "name": "CetylPure",
        "price": 8700,
        "imgName": "product_04.png"
      },
      {
        "id": 5,
        "name": "CLACORE",
        "price": 5600,
        "imgName": "product_05.png"
      },
      {
        "id": 6,
        "name": "POO-POURRI",
        "price": 6800,
        "imgName": "product_06.png"
      },
      {
        "id": 7,
        "name": "Ibuprofen",
        "price": 6800,
        "imgName": "product_07.png"
      }
    ]
  }
  
  

在运行排序时,我不太了解Json的结构。
无论如何,我已经创建了这个提琴,因此您可以检查JSON数组中的正确排序,类似于我期望您在节点后端得到的排序:


在排序中,我看到您正在执行intems.medicine.sort,而您应该执行items.sort,在排序函数中,返回medA.medicine.price

您可以这样进行排序:

const mockJsonData={
“医学”:[
{
“id”:1,
“名称”:“Bioderma”,
“价格”:5000美元,
“imgName”:“product_01.png”
},
{
“id”:2,
“名称”:“Chanca Piedra”,
“价格”:2300美元,
“imgName”:“product_02.png”
},
{
“id”:3,
“名称”:“Umcka”,
“价格”:4000美元,
“imgName”:“product_03.png”
},
{
“id”:4,
“名称”:“十六烷基纯”,
“价格”:8700美元,
“imgName”:“product_04.png”
},
{
“id”:5,
“名称”:“CLACORE”,
“价格”:5600美元,
“imgName”:“product_05.png”
},
{
“id”:6,
“名称”:“POO-POURRI”,
“价格”:6800,
“imgName”:“product_06.png”
},
{
“id”:7,
“名称”:“布洛芬”,
“价格”:6800,
“imgName”:“product_07.png”
}
]
}
const sortByPrice=()=>{
mockJsonData.medicine.sort((a,b)=>a.price-b.price);
}
sortByPrice();
console.log(mockJsonData.medicine);

sortingbyPrice()中的JSON文件是如何读取的?问题更像是我在从教程学习的基础上能够在ejs中显示JSON列表,但我不知道如何将JSON文件连接到ejs进行排序。我不认为是通过testing console.log读取它,$.getJSON中的返回没有任何好处。在这里,您需要用新数据填充组件。如果我只是将json粘贴到ejs中并进行排序,或者在使用json进行排序时建议使用哪种方法?这种排序方法很好。这将不会是一个巨大的开销,你可以依靠它。只需传递json并像普通数组一样对其进行排序。很好,如果我有不同类型的排序,我是否放置项目:JSON.parse(数据)?这不是必需的,JSON.parse将stringify对象转换为JSON(如果它们是有效的)。如果有按字母排序的方法,可以使用其他方法。如果这个答案对你有帮助,你可以对它进行投票。它正确地显示在控制台日志中,但在我单击决战后,它并没有真正显示ejs中的更改。但是谢谢你教我如何分类!
function sortingbyPrice(){
        console.log("Sorting price")
        const itemsJson = JSON.parse(data)
        $.getJSON('items.json', function(data) {
            return sorted = items.medicine.sort((medA, medB) => medA.price - medB.price);
        });
    }   
{
    "medicine": [
      {
        "id": 1,
        "name": "Bioderma",
        "price": 5000,
        "imgName": "product_01.png"
      },
      {
        "id": 2,
        "name": "Chanca Piedra",
        "price": 2300,
        "imgName": "product_02.png"
      },
      {
        "id": 3,
        "name": "Umcka",
        "price": 4000,
        "imgName": "product_03.png"
      },
      {
        "id": 4,
        "name": "CetylPure",
        "price": 8700,
        "imgName": "product_04.png"
      },
      {
        "id": 5,
        "name": "CLACORE",
        "price": 5600,
        "imgName": "product_05.png"
      },
      {
        "id": 6,
        "name": "POO-POURRI",
        "price": 6800,
        "imgName": "product_06.png"
      },
      {
        "id": 7,
        "name": "Ibuprofen",
        "price": 6800,
        "imgName": "product_07.png"
      }
    ]
  }
  
  
console.log("Sorting price")
const itemsJson = [{
  medicine: {

    price: 2
  }
},
{
  medicine: {
    price: 1
  }
}
] 
console.log(itemsJson.sort((medA, medB) => medA.medicine.price 
- medB.medicine.price));