简体   繁体   中英

Change the JSON structure response

I created the json response for the item using this code:

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

it's return the ID of part from my this json:

[{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.

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 .

As well you can use map function after filtering data by manufactureId . 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM