首页 > 解决方案 > 如何过滤与字符串匹配的数组中的数据?

问题描述

我有一个数据数组,我需要将该数据过滤到 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_”字符串的数组数据?

标签: arraysreactjssorting

解决方案


用这个:

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

欲了解更多信息:

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

推荐阅读