Looking up through Array in Actionscript 3.0

本文介绍了如何使用数组方法对包含公司数据的对象进行分类,并通过自定义过滤函数进行搜索操作。

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值