简体   繁体   中英

Filtering data using id

    [
      {
        "acronym": "VMF",
        "defaultValue": "Video & Audio Management Function",
        "description": "This is defined as the Video and/or Audio Management functionality that can be performed on a Digital Item. The Video & Audio Management functions that will exist on the HDEM platform are as follows:\n· CLIP\n· REDACT\n· THUMBNAIL\n· STILL IMAGE\n· AMALGAMATION",
        "id": "5caddba33a87fd7fa5ee601c",
        "name": "Video & Audio Management Function",
        "updatedBy": " ",
        "updatedDate": "2019-04-07T00:00:00Z"
      },
      {
        "acronym": "",
        "defaultValue": "Url Downloadable",
        "description": "Admin configurable header to denote whether the Digital Item/Data within a URL either via Prosecution Readiness or Assign Digital Data can be downloaded.",
        "id": "5caddba33a87fd7fa5ee6098",
        "name": "Url Downloadable",
        "updatedBy": " ",
        "updatedDate": "2019-04-07T00:00:00Z"
      }]

I have this sample json and I want data related to one id how should I get that in react

InfoDescription:_.filter(response, (i) => { return i.id === "passing id here"})

Using this but not working

you can try like this

 let dataToBeFilter=[ { "acronym": "VMF", "defaultValue": "Video & Audio Management Function", "description": "This is defined as the Video and/or Audio Management functionality that can be performed on a Digital Item. The Video & Audio Management functions that will exist on the HDEM platform are as follows:\\n· CLIP\\n· REDACT\\n· THUMBNAIL\\n· STILL IMAGE\\n· AMALGAMATION", "id": "5caddba33a87fd7fa5ee601c", "name": "Video & Audio Management Function", "updatedBy": " ", "updatedDate": "2019-04-07T00:00:00Z" }, { "acronym": "", "defaultValue": "Url Downloadable", "description": "Admin configurable header to denote whether the Digital Item/Data within a URL either via Prosecution Readiness or Assign Digital Data can be downloaded.", "id": "5caddba33a87fd7fa5ee6098", "name": "Url Downloadable", "updatedBy": " ", "updatedDate": "2019-04-07T00:00:00Z" }] let filteredData=dataToBeFilter.filter(obj=>obj.id=="5caddba33a87fd7fa5ee6098") console.log(filteredData)

I don't use lodash.

If are just looking for one specific ID in an array I would do it using the native find method like this. If you want to get multiple replace find with filter .

const getById = arr => id => arr.find(x => x.id === id)

There's a syntax error on your filter function usage. The first parameter should be a function that return a boolean which determine if the item should included to the new returned Array.

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