简体   繁体   English

更改JSON结构响应

[英]Change the JSON structure response

I created the json response for the item using this code: 我使用以下代码为该项目创建了json响应:

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

it's return the ID of part from my this json: 从我的这个json返回part的ID:

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

I also tried using this code but not working :(": 我也尝试使用此代码,但不起作用:(“:

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

What I want to achieved is, the response will parse the item name instead of part.id. 我要实现的是,响应将解析项目名称而不是part.id。

As pointed in comments you can change you last code bit to 如注释中所指出的,您可以将最后一个代码位更改为

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

You were missing parenthesis to call a function. 您缺少括号来调用函数。 And you might need to filter by manufactureId , not id . 并且您可能需要按manufactureId而不是id进行过滤。

As well you can use map function after filtering data by manufactureId . 同样,您也可以在按manufactureId过滤数据后使用map功能。 map function applies function for each array element and return result, in this example it simple get name from object, because there might be more that one (or none) item by manufactureId . 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