【问题标题】:Filtering data using id使用 id 过滤数据
【发布时间】:2019-04-12 05:59:36
【问题描述】:
    [
      {
        "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 相关的数据,我应该如何在反应中得到它

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

使用这个但不工作

【问题讨论】:

  • 为什么不 dataToFilter.filter(item => item.id === "passing id here?")
  • 请分享来自控制台的错误,或者任何关于什么不工作的细节?
  • 一切正常in the fiddle
  • 我也执行了这段代码 sn-p 并且运行良好。

标签: javascript reactjs lodash


【解决方案1】:

你可以这样试试

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)

  

【讨论】:

  • 谢谢,但是当我试图从数据中显示默认值时它没有给出任何东西,我正在使用 this.setstate({ data:filteredData)} 并试图显示 data.defaultValue
【解决方案2】:

我不使用 lodash。

如果只是在数组中寻找一个特定的 ID,我会使用像这样的原生 find 方法。如果您想获得多个替换 findfilter

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 语法为_.filter(collection, [predicate=_.identity])Documentation
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多