Looking up through Array in Actionscript 3.0
2011-10-05 18:05
363 查看
TASK DESCRIPTION:
I have a Company class, holding some data of a company, such as English name, Chinese name and on which floor it is located. It has no method, so it
functions exactly in the same way as struct in C/C++. And I have an array of Company objects.
I've got two missions now. The first one is to classify the Company objects into different arrays, by its one certain property, say, business nature. And the second one is to perform searching throughout
the array of Company.
Mission 1: Classification
This one is pretty simple. First of all we need a loop on the array; then inside each step, check if we already record on the Company's business nature, if yes, we push it in to the array for that
business nature, if not we store that business and push the Company into a brand new array.
So we need:
an array of business: _businessNatures,
a
2-dimesion array, its elements are arrays of Company, one per one business nature, the business string is stored in_businessNature array with the same index:
_byBussNature;
an index keep track of the current index of the array we are accessing: $idx_flr
Nothing special, but just involves the usage of indexOf method of Array, it will return
the index of the value you find, otherwise return -1, to incidate that the value doesn't exist. I use this method to find if the string present business nature have already been around there.
Mission 2: Searching
This task is done with filter method of Array. You need to design your own filter function,
which must return Boolean type value, and inside the body you define your own comparision criteria. And compiler will use your criteria to perform the comparision on each element of the array, then return the ones meet it.
Highlights In the end
The Company objects are complex data type, so the two new classfied arrays hold the reference to the values stored in _companyList array, instead of new copys. Manipulation on the former will affect the values in the later. Paste
the piece and run:
The output:
download the .fla
REFS:
http://www.onebyonedesign.com/tutorials/array_methods/
http://forums.adobe.com/message/3954482
I have a Company class, holding some data of a company, such as English name, Chinese name and on which floor it is located. It has no method, so it
functions exactly in the same way as struct in C/C++. And I have an array of Company objects.
I've got two missions now. The first one is to classify the Company objects into different arrays, by its one certain property, say, business nature. And the second one is to perform searching throughout
the array of Company.
Mission 1: Classification
This one is pretty simple. First of all we need a loop on the array; then inside each step, check if we already record on the Company's business nature, if yes, we push it in to the array for that
business nature, if not we store that business and push the Company into a brand new array.
So we need:
an array of business: _businessNatures,
a
2-dimesion array, its elements are arrays of Company, one per one business nature, the business string is stored in_businessNature array with the same index:
_byBussNature;
an index keep track of the current index of the array we are accessing: $idx_flr
package { ...... import model.Company; public class main extends Sprite { ...... private var _companyList:Array; private var _businessNatures:Array; private var _byBussNature:Array; public function main() { ...... } ...... private function classifyArray():void { this._businessNatures = new Array(); this._byBussNature = new Array(); var $idx_bntr:int; for(var $i:int = 0; $i < this._companyList.length; $i++) { $idx_bntr = this._businessNatures.indexOf(this._companyList[$i].bntr); if( -1 == $idx_bntr ) { this._businessNatures.push( this._companyList[$i].bntr ); var $tempArray_bntr:Array = new Array(); $tempArray_bntr.push( this._companyList[$i] ); this._byBussNature.push( $tempArray_bntr ); } else { this._byBussNature[$idx_bntr].push( this._companyList[$i] ); } } } ...... } }
Nothing special, but just involves the usage of indexOf method of Array, it will return
the index of the value you find, otherwise return -1, to incidate that the value doesn't exist. I use this method to find if the string present business nature have already been around there.
Mission 2: Searching
This task is done with filter method of Array. You need to design your own filter function,
which must return Boolean type value, and inside the body you define your own comparision criteria. And compiler will use your criteria to perform the comparision on each element of the array, then return the ones meet it.
package { ...... import model.Company; public class main extends Sprite { ...... private var _companyList:Array; private var _searchValue_Alph:String; public function main() { ...... } ...... private function filterArray():void { var $searchResult:Array = this._companyList.filter(alphaFilter); } private function alphaFilter($element:Company, $index:int, $arr:Array):Boolean { var $low:String = Company($element).e.toLowerCase(); return ( $low.indexOf( this._searchValue_Alph.toLowerCase() ) > 0 ); } ...... } }
Highlights In the end
The Company objects are complex data type, so the two new classfied arrays hold the reference to the values stored in _companyList array, instead of new copys. Manipulation on the former will affect the values in the later. Paste
the piece and run:
public function main() { classifyArray(); trace(this._companyList[178].c); trace(this._byBussNature[4][10].c); this._byBussNature[4][10].c = "中國聯通"; trace(this._companyList[178].c); }
The output:
中國電信(香港)國際有限公司 中國電信(香港)國際有限公司 中國聯通
download the .fla
REFS:
http://www.onebyonedesign.com/tutorials/array_methods/
http://forums.adobe.com/message/3954482
相关文章推荐
- Detect Chinese character through out a string in Actionscript3.0
- Loop Through Object in Actionscript3.0
- Alternatives to Array in Actionscript3.0
- Constructor Overloading in Actionscript 3.0
- Capture Keyboard Event in Actionscript3.0
- Bitmap & BitmapData classes in Actionscript 3.0
- root, stage and timeline in ActionScript3.0
- Create a Drag and Drop Puzzle in ActionScript 3.0
- Custom Event in ActionScript3.0
- Reflection in Actionscript 3.0/Flex 2
- Essential ActionScript 3.0 Part ⅠActionScript from the Ground Up Review
- Empty, Unavailable, Invalid Variables Handling in Actionscript 3.0 & PHP 5+
- Garbage Collection in Actionscript3.0
- NativeScript官方书籍:NativeScript in Action-用你现有技术构建移动应用程序
- flash actionscript 3.0 发布H264的视频流
- Stop all sounds with Actionscript 3.0
- Best Way to Loop Through an Array in JavaScript
- Flex ActionScript 3.0 SharedObject 本地共享对象 操作
- Flash ActionScript 3.0 学习开篇
- [ActionScripte 3.0] swf和网页通信