使用ID过滤数据

时间:2019-04-12 05:59:37

标签: javascript reactjs lodash

    [
      {
        "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"
      }]

我有这个示例json,我想要与一个id相关的数据,如何在React中得到它

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

使用它但不起作用

3 个答案:

答案 0 :(得分:2)

您可以尝试这样

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)

  

答案 1 :(得分:0)

我不使用lodash。

如果只是在数组中查找一个特定的ID,则可以使用本机find这样的方法来完成。如果要获取多个,请用find替换filter

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

答案 2 :(得分:0)

您的过滤器功能用法存在语法错误。第一个参数应该是一个返回布尔值的函数,该布尔值确定是否应将该项目包括在新返回的Array中。