首页 > 解决方案 > 如何将json文件连接到nodejs进行排序

问题描述

我从来没有在 json 和 nodejs 和 ejs 之前使用过,所以请原谅我缺乏知识。我能够从 json 生成项目列表,但是当我按下下拉列表进行排序时,除了基于下面的 ejs 按 id 进行默认排序之外,它没有显示任何输出。我错误地连接了json吗?

节点

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

<select id="sortMed" onChange="getSelectValue()">
                <option onClick="defaultSorting()">Default Sorting</option>
                <option onClick="sortingbyPrice()">Sorting by price</option>
                <option onClick="sortingbyAlphabet()">Sorting by alphabet</option>
            </select> 

   <div class="row">
            <div class="col-4">
                <div class="shop-items" id="output">
                    <% items.medicine.forEach(function(item){ %>
                        <div class="shop-item" data-item-id="<%= item.id %>">
                            <span class="shop-item-title"><%= item.name %></span>
                            <img class="shop-item-image" src="../assets/img/<%= item.imgName %>">
                            <div class="shop-item-details">
                                <span class="shop-item-price">$<%= item.price / 100 %></span>
                                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                            </div>
                        </div>
                    <% }) %>
                </div>
            </div>
        </div>

javascript

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

项目.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"
      }
    ]
  }
  
  

标签: javascriptnode.jsjsonejs

解决方案


您可以像这样进行排序:

const mockJsonData = {
    "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"
      }
    ]
  }
  
   const sortByPrice = () => {
      mockJsonData.medicine.sort((a, b) =>  a.price - b.price);
 }
sortByPrice();
console.log(mockJsonData.medicine);

  

现在,你已经得到了排序好的药物列表,用这个更新的值重新渲染 ejs 模板。

res.render('product.ejs', {
                    user: req.user,
                    stripePublicKey: stripePublicKey,
                    items: sortingbyPrice(data) // data from reading JSON file
                });

// you sorting function be like


function sortingbyPrice(data){
        return itemsJson.medicine.sort((a, b) =>  a.price - b.price);
}
 

此外,您可以在此处查看


推荐阅读