簡體   English   中英

更改JSON結構響應

[英]Change the JSON structure response

我使用以下代碼為該項目創建了json響應:

this.parts = this.getParts().filter((item) => {
  return item.manufactureId === Number(manufactureId)
  });

從我的這個json返回part的ID:

[{id: 02020013, manufactureId: 0202, name: cable}]

我也嘗試使用此代碼,但不起作用:(“:

this.parts = this.getParts.filter(p => (p.id == manufactureId))[0].name;

我要實現的是,響應將解析項目名稱而不是part.id。

如注釋中所指出的,您可以將最后一個代碼位更改為

this.parts = this.getParts().filter(p => (p.manufactureId == manufactureId))[0].name;

您缺少括號來調用函數。 並且您可能需要按manufactureId而不是id進行過濾。

同樣,您也可以在按manufactureId過濾數據后使用map功能。 map函數對每個數組元素應用函數並返回結果,在此示例中,它簡單地從object獲取name ,因為manufactureId可能包含多個(或沒有)項。

this.parts = this.getParts().filter((item) => {
    return item.manufactureId === Number(manufactureId)
}).map((item) => item.name);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM