正确实现字符串“in”的更好方法是大批
假设您有一个包含 ActionScript3 中的多个字符串的数组,并且您想要测试测试字符串是否在该数组“中”。 “in”仅适用于 AS3 中数组的索引(如果你问我的话,这完全是迟缓的),尽管它确实适用于对象,但我们不是在谈论对象。
有人可以改进(减少)我现在使用的这段代码吗?我希望避免定义实用函数 - 我想要一个漂亮优雅的单行代码。
myArray.filter(function(item:*, i:int, a:Array) { return (item == testString); }).length
由于 0 == false 我们可以在测试中使用它。请注意,testString 的范围是在包含函数中定义的,并由闭包封装。
if (allowedProfiles.filter(function(item:*, i:int, a:Array) { return (item == name); }).length){ // yay! }
suppose you have an array with a number of strings in ActionScript3 and you want to test if a test string is "in" that array. "in" only works against the index with Arrays in AS3 (which is totally retardo if you ask me), though it does work with ojects, but we're not talking about objects.
Can someone improve (reduce) on this code I'm using now? I'm hoping to avoid defining a utility function - I'd like a nice elegant one-liner.
myArray.filter(function(item:*, i:int, a:Array) { return (item == testString); }).length
Since 0 == false we can use it in a test. Do note that testString's scope is defined in the containing function, encapsulated by the closure.
if (allowedProfiles.filter(function(item:*, i:int, a:Array) { return (item == name); }).length){ // yay! }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Array.indexOf()
方法可以看到字符串在数组中的索引不是-1
(未找到):Use the
Array.indexOf()
method to see that the index of the string in the array is not-1
(not found):为什么不直接使用 indexOf()?
Why not just use indexOf()?