Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript simplest way to check if item exists in array like C# Linq Any ( using any library )

Tags:

typescript

TypeScript simplest way to check if item exists in array like C# Linq Any ( using any library ). Something like

var myArray=[1,2,3,4];    var is_3_Exist=myArray.Any(x=> x==3);
like image 716
JavaScript Linq Avatar asked Aug 17 '15 06:08

JavaScript Linq


People also ask

How do you check if an item exists in an array TypeScript?

The includes() method determines whether an array contains a specified element. This method returns true if the array contains the element, and false if not.

How do you check if an item exists in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if an object exists in an array of objects?

Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.

How do you check if an array contains a value in C?

To check if given Array contains a specified element in C programming, iterate over the elements of array, and during each iteration check if this element is equal to the element we are searching for.


2 Answers

Use .some:

myArray.some(x=>x==3);

like image 50
basarat Avatar answered Sep 18 '22 12:09

basarat


FIDDLE: https://jsfiddle.net/vktawbzg/

NPM: https://www.npmjs.com/package/linqscript

GITHUB: https://github.com/sevensc/linqscript

Take a look at my Github Repository. It would be great if you guys can help to improve it! https://github.com/sevensc/linqscript

Syntax:

list.Any(c => c.Name === ("Apple")) 
like image 45
seven Avatar answered Sep 20 '22 12:09

seven