2016-11-25 60 views
0

我想選擇與此特定ID匹配的數據。我已經做了一個API調用,它給了我這個JSON數據,但我只想要ID爲eb9fa7e9-3dc9-11e6-a9f9-28e347的數據。我如何過濾這些數據?按特定ID過濾數據以在ReactJS中顯示

 [ 
      { 
      "food_item_id": "a095eca7-3dcf-11e6-a9f9-28e347", 
      "food_group_id": "eb9fa7e9-3dc9-11e6-a9f9-28e347", 
      "food_item_name": "Don't know jack" 
      }, 
      { 
      "food_item_id": "a09b073d-3dcf-11e6-a9f9-28e347", 
      "food_group_id": "ebaeef2c-3dc9-11e6-a9f9-28e347", 
      "food_item_name": "Oreo cookie monster" 
      }, 
      { 
     "food_item_id": "a180332e-3dcf-11e6-a9f9-28e347", 
     "food_group_id": "ab9fa7e9-3dc9-11e6-a9f9-69e8124", 
     "food_item_name": "Big Country"     
     } 
     ] 

如何通過"food_group_id": "eb9fa7e9-3dc9-11e6-a9f9-28e347"過濾:

var canvases = this.state.foodinfo.map(function(data) { 
     return (
      <div> 
      <Card className="col-xs-12 col-sm-4 eachCard"> 
      <CardImg top width="100%" src={data.food_item_pic} alt="Card image cap" /> 
      <CardBlock className="cardBlock"> 
       <CardTitle>{data.food_item_name}</CardTitle> 
       <CardSubtitle>${data.food_item_price}</CardSubtitle> 
       <span class="glyphicon glyphicon-plus-sign btnClass"></span> 
      </CardBlock> 
      </Card> 
      </div> 
     ); 
     }); 

map前添加一個過濾器,但我對數組的項目得到一個錯誤:

var canvases = this.state.foodinfo.filter(item => food_group_id=== 'eb9fa7e9-3dc9-11e6-a9f9-28e347').map(function(data) { 
      return (
       <div> 
       <Card className="col-xs-12 col-sm-4 eachCard"> 
       <CardImg top width="100%" src={data.food_item_pic} alt="Card image cap" /> 
       <CardBlock className="cardBlock"> 
        <CardTitle>{data.food_item_name}</CardTitle> 
        <CardSubtitle>${data.food_item_price}</CardSubtitle> 
        <span class="glyphicon glyphicon-plus-sign btnClass"></span> 
       </CardBlock> 
       </Card> 
       </div> 
      ); 
      }); 
+0

'.filter(item => item.food_group_id ==='...').map' – pawel

回答

1

應該」它是filter(item => item.food_group_id ===

+0

非常感謝你的工作 – Sidhemu

相關問題