1

I have an array of data and I need to filter that data into a variable in react js.

This is my array

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

I need to get the data that contain "idappcontent == "Sign_" ". How to filter array data that match "Sign_" string?

Sidath
  • 379
  • 1
  • 9
  • 25
  • 1
    Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Amruth Aug 03 '21 at 05:54
  • `array.filter(o => o.idappcontent.startsWith("Sign_"))` – Hassan Imam Aug 03 '21 at 06:03

3 Answers3

5

use this:

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

for more info:

  1. The filter() method creates a new array with all elements that pass the test implemented by the provided function.
  2. The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
Abbasihsn
  • 2,075
  • 1
  • 7
  • 17
3

You can combine the javascript filter method with the includes method to filter the results.

Filter is a method that accepts a condition to filter the data; Includes is a method that searches for the given value in an array (yes, string is an array-like type).

const signResults = array.filter(id => id.idappcontent.includes("Sign_"));
Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
2

You can try following :

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