In .NET i can do:
var ls = new List<string>(1,2,3,3,3,4,5,6);
var x1 = ls.Single(x => x == 3); // throws exception because there are more elems of 3 defined in ls
var x2 = ls.SingleOrDefault(x => x==3) // Returns default of int (0) because there are more elems of 3 defined in ls
var x3 = ls.Single(x => x== 1) // returns 1
Single documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single?view=netcore-3.1
What would be the Typescript equivalent?
I'm working in the latest version of Angular.
Array#find()will give you a single item based on a predicate. If it nothing matches, you getundefined. It's basically.First(), since it doesn't fail with multiple matches. There is no direct equivalent for.Single()- you can use other libraries (some even add LINQ type methods in JS), or implement the functionality it yourself.