TypeScript 检查项目是否存在于数组中的最简单方法,如 C# Linq Any(使用任何库)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32043894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript simplest way to check if item exists in array like C# Linq Any ( using any library )
提问by JavaScript Linq
TypeScript simplest way to check if item exists in array like C# Linq Any ( using any library ). Something like
TypeScript 检查项目是否存在于数组中的最简单方法,如 C# Linq Any(使用任何库)。就像是
var myArray=[1,2,3,4];
var is_3_Exist=myArray.Any(x=> x==3);
回答by basarat
Use .some
:
使用.some
:
myArray.some(x=>x==3);
myArray.some(x=>x==3);
回答by seven
FIDDLE:https://jsfiddle.net/vktawbzg/
小提琴:https ://jsfiddle.net/vktawbzg/
NPM:https://www.npmjs.com/package/linqscript
NPM:https : //www.npmjs.com/package/linqscript
GITHUB:https://github.com/sevensc/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
看看我的 Github 存储库。如果大家能帮忙改进一下就好了! https://github.com/sevensc/linqscript
Syntax:
句法:
list.Any(c => c.Name === ("Apple"))
回答by abahet
You can use the findindex method :
您可以使用 findindex 方法:
if( myArray.findIndex(x => x === 3) >= 0) {
// foud myArray element equals to 3
}
回答by Nypan
If this is the only thing you need to do you should go for .some (with polyfill) if you however want Linq functionality for other things as well you should take a look at https://github.com/ReactiveX/IxJS.
如果这是您唯一需要做的事情,您应该选择 .some(使用 polyfill),如果您还希望 Linq 功能用于其他事情,您应该查看https://github.com/ReactiveX/IxJS。