as3 Vector.map 和 Vector.filter
浏览了现场文档,但示例非常稀疏。 谁能给我一些“现实世界”的好例子,说明您何时以及为什么可以使用 Actionscript 中新 Vector 类的映射和过滤方法?
Gone thru the livedocs but the examples are quite sparse.
Can anyone give me some good 'real world' examples of when and why you might use the
map and filter methods of the new Vector class in Actionscript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Vector
filter
和map
方法的工作方式与同名的 Array 类中的方法相同。检查以下两页的数组过滤器示例(它们对于 Vector 类的工作原理相同):http://troyworks.com/blog/2007/12/16/as3-arrayfilter-r0xr/
http://www.onebyonedesign.com/tutorials/array_methods/
我找不到任何不错的 as3 地图示例,但 jQuery 等效项解释了如何/为什么你会很好地使用它:
http://api.jquery.com/jQuery.map/
至于现实世界的使用,过滤器非常有用。假设您有一个充满 Employee 对象的 Vector。您可以使用
filter
方法作为非破坏性方法来获取所有姓名以“A”开头或工资大于 $50,000 的员工。map
函数类似,但它创建一个新的 Vector 并允许您更改对象,因此您可以将每个 Employee 转换为新类型,例如 HighPaidEmployee。The Vector
filter
andmap
methods work just the same as the methods in the Array class of the same name. Check the following two pages for filter examples for arrays (they work just the same for the Vector class):http://troyworks.com/blog/2007/12/16/as3-arrayfilter-r0xr/
http://www.onebyonedesign.com/tutorials/array_methods/
I couldn't find any decent as3 map examples, but the jQuery equivalent explains how/why you would use it fairly well:
http://api.jquery.com/jQuery.map/
As for real world uses, filter is unbelievably useful. Suppose you have a Vector filled with Employee objects. You can use the
filter
method as a non-destructive way to get all the Employees whose name starts with "A", or whose salary is greater than $50,000. Themap
function is similar, but it creates a new Vector and allows you to alter the objects, so you could potentially cast each Employee as a new type, such as HighlyPaidEmployee.