How can I check if an array contains a particular string in TypeScript

0 votes
With the help of code example can you tell me How can I check if an array contains a particular string in TypeScript?
Feb 10 in Node-js by Ashutosh
• 20,830 points
71 views

1 answer to this question.

0 votes

You can use includes() method, which checks for the presence of an element and returns a boolean value.

Here's how you can use it:

const fruit: string[] = ['apple', 'banana', 'mango'];

const hasMango: boolean = fruit.includes('mango');

console.log(hasMango); // Output: true

const hasOrange: boolean = fruit.includes('orange');

console.log(hasOrange); // Output: false

In this example, fruit.includes('mango') returns true because 'mango' is an element in the fruits array, while fruits.includes('orange') returns false because 'orange' is not present.

answered Feb 10 by Navya

Related Questions In Node-js

0 votes
0 answers

How can I implement a nested if-else statement in ReactJS JSX?

Can you tell me How can I ...READ MORE

14 hours ago in Node-js by Ashutosh
• 20,830 points
5 views
0 votes
0 answers
0 votes
1 answer

How to check if a collection exists in Mongodb native nodejs driver?

Hello @kartik, The collectionNames method of the native driver's Db object accepts ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,840 points
14,500 views
0 votes
1 answer

How do I perform an id array query in Mongoose?

Hello @kartik, Here is a mongoosey way to ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
6,383 views
0 votes
1 answer

How do I transform an array into an object?

Here are some common approaches: Using Array.prototype.reduce(): Example: const array ...READ MORE

answered Feb 10 in Node-js by Navya
64 views
0 votes
1 answer

How do I set Resharper's language level for ECMAScript 6?

To configure ReSharper to recognize ECMAScript 6 ...READ MORE

answered Feb 10 in Node-js by Navya
68 views
0 votes
1 answer

What are the practical applications of ES6 WeakMap?

Practical Applications of WeakMap: Private Data Storage: Store private ...READ MORE

answered Feb 10 in Node-js by Navya
68 views
0 votes
1 answer

What is the correct method to clone a JavaScript object?

Cloning a JavaScript object can be achieved ...READ MORE

answered Feb 10 in Node-js by Navya
108 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP