0

I am mapping over some data that I am getting from a api however when i try to add the filter function i get 'currencyData.includes is not a function'

I have also tried just hard coding the array but it also still doesnt work?

I have a loading state for when i fetch data from the api which holds code from being run but i have removed it from this example as its not getting data from the api below.

The simplified version is here...

ARRAY

var items = [
        {
          "id": 1,
          "productName": "shoes",
          "productIdentifier": "CL001",
          "productDescription": "adidas kicks boir",
          "productPrice": 2000,
          "productStock": 200,
          "created_at": "2020-51-28",
          "updated_at": null
         },
        {
          "id": 2,
          "productName": "burger",
          "productIdentifier": "FD001",
          "productDescription": "charsiu berger",
          "productPrice": 2000,
          "productStock": 200,
          "created_at": "2020-51-28",
          "updated_at": null
         }
      ]
return(
   {items.filter(currencyInfo => currencyInfo.includes("FD001")).map((value, index) => {
        console.log(value)
        return(
              <h1 key={index}>{value}</h1>
              )
    })}
)
1
  • That's totally normal, cause currencyInfo is an object, not an array, you can't call .includes() on it. Commented Aug 27, 2021 at 9:34

2 Answers 2

4

currencyInfo is not an array, you can not call includes on it

Here is my suggestion:

return(
   {items.filter(currencyInfo => currencyInfo.productIdentifier === "FD001").map((value, index) => {
        console.log(value)
        return(
              <h1 key={index}>{value}</h1>
              )
    })}
)

More about includes()

Sign up to request clarification or add additional context in comments.

1 Comment

Ah so simple now I see the answer. Thanks :)
1

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. Check this Doc

But in items.filter(currencyInfo => currencyInfo.includes("FD001")), type of currencyInfo isn't array but object.

So you should use currencyInfo.productIdentifier.includes()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.