close

[Solved] How do I check whether an array contains a string in TypeScript?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How do I check whether an array contains a string in TypeScript? in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How do I check whether an array contains a string in TypeScript Error Occurs?

Today I get the following error How do I check whether an array contains a string in TypeScript? in javascript.

How To Solve How do I check whether an array contains a string in TypeScript Error ?

  1. How To Solve How do I check whether an array contains a string in TypeScript Error ?

    To Solve How do I check whether an array contains a string in TypeScript Error You can use filter too
    this.products = array_products.filter((x) => x.Name.includes("ABC"))

  2. How do I check whether an array contains a string in TypeScript?

    To Solve How do I check whether an array contains a string in TypeScript Error You can use filter too
    this.products = array_products.filter((x) => x.Name.includes("ABC"))

Solution 1

Also note that “in” keyword does not work on arrays. It works on objects only.

propName in myObject

Array inclusion test is

myArray.includes('three');

Solution 2

You can use filter too

this.products = array_products.filter((x) => x.Name.includes("ABC"))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment