【问题标题】:How to filter data in an array that match a string?如何过滤与字符串匹配的数组中的数据?
【发布时间】:2021-10-08 08:36:32
【问题描述】:

我有一个数据数组,我需要将该数据过滤到 react js 中的一个变量中。

这是我的数组

[{idappcontent: "Com_01", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Com_02", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Com_03", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Review1_Ques_01", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Review1_Ques_02", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Sign_01", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Sign_02", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Thank_01", idcontenttype: "0", idclient: "5"},
 {idappcontent: "Thank_02", idcontenttype: "0", idclient: "5"}
]

我需要获取包含“idappcontent ==”Sign_””的数据。如何过滤匹配“Sign_”字符串的数组数据?

【问题讨论】:

标签: arrays reactjs sorting


【解决方案1】:

使用这个:

const temp = YOURDATA.filter(item=> item.idappcontent.includes("Sign_"));

更多信息:

  1. filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
  2. includes() 方法执行区分大小写的搜索,以确定是否可以在另一个字符串中找到一个字符串,并根据需要返回 true 或 false。

【讨论】:

    【解决方案2】:

    您可以将javascript filter 方法与includes 方法结合起来过滤结果。

    Filter是一种接受条件过滤数据的方法; Includes 是一种在数组中搜索给定值的方法(是的,字符串是类似数组的类型)。

    const signResults = array.filter(id => id.idappcontent.includes("Sign_"));
    

    【讨论】:

      【解决方案3】:

      您可以尝试以下操作:

      dataArray.filter(item => item.idappcontent.includes("Sign_"));
      

      【讨论】:

        猜你喜欢
        • 2018-11-27
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2021-04-28
        • 2021-12-15
        • 2013-06-08
        • 2020-07-17
        • 1970-01-01
        相关资源
        最近更新 更多