亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

鑒于產品位于兩個單獨的 json 文件中,如何使用 jquery 根據類別過濾產品?

鑒于產品位于兩個單獨的 json 文件中,如何使用 jquery 根據類別過濾產品?

慕尼黑5688855 2022-01-20 18:56:59
好吧,這對我來說有點棘手。我有兩個 json 文件,food.json 和 rest_category.json。我試圖找到一種方法來根據 rest_category.json 中的類別過濾 food.json 中的食物。我有一個想法,也許我應該在 food.json 中添加一個與 rest_category.json 中的類別中的 id 匹配的 foreign_id。不過,我該如何從那里開始?有沒有辦法我可以嘗試這個?我真的很感激。謝謝 下面是我的 food.json 和 rest_category.json 的示例。食物.json[{    "id":"1",    "Name":"Coke 500ml",    "Price":"80",    "Quantity": "50",    "Category":"Beverages"},{    "id":"2",    "Name":"Cake",    "Price":"150",    "Quantity": "40",    "Category":"Appetizer"},{    "id":"3",    "Name":"Beef Ribs",    "Price":"100",    "Quantity": "50",    "Category":"Side Items"},{    "id":"4",    "Name":"Cabbage Salad",    "Price":"50",    "Quantity": "30",    "Category":"Salads"},{    "id":"5",    "Name":"Cake",    "Price":"150",            "Quantity": "30",    "Category":"Appetizer"},{    "id":"6",    "Name":"Beef Ribs",    "Price":"100",    "Quantity": "30",    "Category":"Side Items"}]rest_category.json[{    "id" : "1",    "name" : "Appetizers",    "Status" : "Active"   },{    "id" : "2",    "name" : "Salads",    "Status" : "Active"         },{    "id" : "3",    "name" : "Entrees",    "Status" : "Active"    },{    "id" : "4",    "name" : "Side Items",    "Status" : "Active"    },{    "id" : "5",    "name" : "Beverages",    "Status" : "Active" }]補充一點,響應應該顯示在 HTML 頁面上。我目前能夠附加類別和食物的列表??ㄔ谶^濾器部分。下面是我用來附加類別和食物的示例代碼。
查看完整描述

2 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

只需使用Array.map和Array.filter:


let products = [


{

    "id":"1",

    "Name":"Coke 500ml",

    "Price":"80",

    "Quantity": "50",

    "Category":"Beverages"

},


{

    "id":"2",

    "Name":"Cake",

    "Price":"150",

    "Quantity": "40",

    "Category":"Appetizer"

},


{

    "id":"3",

    "Name":"Beef Ribs",

    "Price":"100",

    "Quantity": "50",

    "Category":"Side Items"

},


{

    "id":"4",

    "Name":"Cabbage Salad",

    "Price":"50",

    "Quantity": "30",

    "Category":"Salads"

},


{

    "id":"5",

    "Name":"Cake",

    "Price":"150",        

    "Quantity": "30",

    "Category":"Appetizer"

},


{

    "id":"6",

    "Name":"Beef Ribs",

    "Price":"100",

    "Quantity": "30",

    "Category":"Side Items"

}

];


let categories = [

{

    "id" : "1",

    "name" : "Appetizers",

    "Status" : "Active"   

},


{

    "id" : "2",

    "name" : "Salads",

    "Status" : "Active"         

},


{

    "id" : "3",

    "name" : "Entrees",

    "Status" : "Active"    

},


{

    "id" : "4",

    "name" : "Side Items",

    "Status" : "Active"    

},


{

    "id" : "5",

    "name" : "Beverages",

    "Status" : "Active" 

}

];


let result = categories.map(c=>{

  return {

    ...c,

    products : products.filter(p=>p.Category===c.name)

  }

})


console.log(JSON.stringify(result,null,2));


查看完整回答
反對 回復 2022-01-20
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

您應該嘗試使用Array.filter()方法:


const arr = [


  {

    "id": "1",

    "Name": "Coke 500ml",

    "Price": "80",

    "Quantity": "50",

    "Category": "Beverages"

  },


  {

    "id": "2",

    "Name": "Cake",

    "Price": "150",

    "Quantity": "40",

    "Category": "Appetizer"

  },


  {

    "id": "3",

    "Name": "Beef Ribs",

    "Price": "100",

    "Quantity": "50",

    "Category": "Side Items"

  },


  {

    "id": "4",

    "Name": "Cabbage Salad",

    "Price": "50",

    "Quantity": "30",

    "Category": "Salads"

  },


  {

    "id": "5",

    "Name": "Cake",

    "Price": "150",

    "Quantity": "30",

    "Category": "Appetizer"

  },


  {

    "id": "6",

    "Name": "Beef Ribs",

    "Price": "100",

    "Quantity": "30",

    "Category": "Side Items"

  }

];


function filterArrayByCategory(category) {

  return arr.filter(el => el.Category === category);

}


console.log(filterArrayByCategory('Appetizer'));


查看完整回答
反對 回復 2022-01-20
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號