Asv3's Blog

October 17, 2009

Filtering Array For Objects with Unique values in ActionScript

Filed under: actionscript, flash, flex — Tags: , , , , , , — asv3 @ 8:33 am

I happened to go through many instances where I had to filter a flat list on given attribute for unique values. For example I have flat list of employees who is working in different territory with there achievements, problem is show the graph based on territory. In flex you can use GroupedColletion to achieve this, but if you doing it in flash you are left with no options other than Array.filter. Here is how I did it.

var flatData:Array = [

{ Region:"Southwest", Territory:"Arizona", Territory_Rep:"Barbara Jennings", Estimate:40000 , Actual:38865 },

{ Region:"Southwest", Territory:"Arizona", Territory_Rep:"Dana Binn", Estimate:30000 , Actual:29885 },

{ Region:"Southwest", Territory:"Central California", Territory_Rep:"Joe Schmoe" , Estimate:30000 , Actual:29134 },

{ Region:"Southwest", Territory:"Northern California" , Territory_Rep:"Lauren Ipsum" , Estimate:40000 , Actual:38805 },

{ Region:"Southwest", Territory:"Northern California" , Territory_Rep:"T.R. Smith" , Estimate:40000 , Actual:55498 },

{ Region:"Southwest", Territory:"Southern California" , Territory_Rep:"Jane Grove" , Estimate:45000 , Actual:44913 },

{ Region:"Southwest", Territory:"Southern California" , Territory_Rep:"Alice Treu" , Estimate:45000 , Actual:44985 },

{ Region:"Southwest", Territory:"Nevada" , Territory_Rep:"Bethany Pittman", Estimate:45000 , Actual:52888 }

];

 

// sort first to make sure filter logic doesn’t fail

flatData.sortOn("Territory");

// function to return true for false for filtering

function is_unique_ter(item:*, index:int, arr:Array) {

    if (index<arr.length1) {

        return item.Territory!=arr[index+1].Territory;

    } else {

        return true;

    }

 

}

 

//assign filtered data to different array

var filteredflatData:Array=flatData.filter(is_unique_ter);

 

// trace out contents of filteredflatData

for (var i:Object in filteredflatData) {

    var s="";

    for (var j:String in filteredflatData[i]) {

        s+=( filteredflatData[i][j])+",";

    }

    trace(s);

}

it should give trace output like this:

30000,Southwest,Arizona,29885,Dana Binn,
30000,Southwest,Central California,29134,Joe Schmoe,
45000,Southwest,Nevada,52888,Bethany Pittman,
40000,Southwest,Northern California,55498,T.R. Smith,
45000,Southwest,Southern California,44913,Jane Grove,

September 27, 2009

Display of display attribute in CSS

Filed under: css — Tags: , , , , , , , , — asv3 @ 3:11 am

block, inline and none are the only 3 values supported by all browsers, inline-block is not supported in IE5.5 and FF2, and IE6/IE7 respect inline-block only when it’s assigned to natural inline elements. list-item is supported in all browsers except IE5.5, run-in is buggy in almost all browsers except IE8+ and Opera 9.62+

I suggest you block, inline and none freely where-ever you can, and inline-block if you are targeting IE8+ and FF3+, it’s better not to use rest of the values. Also note <img> is an inline element.

display:block

The element displayed as a block, like <p>, <div>, <h[1-5]>. Block level elements have default display set to block. Block-level elements and elements that have display set to block, always starts from new line/block, unless you float it.

display:inline

inline used to make an block level element behave like an inline element.  width attribute of inline elements are often ignored, whichever element need to support width attribute that should not have display set to inline.

display:none

used to remove an element from document flow, browser skips rendering all elements set to display none, often used to hide/unhide elements in page.

display:inline-block

used when you have to specify width to an inline elements. Refer to the limitations of this property said above.

Others, run-in, list-item, compact, table, table-row, table-cell, table-caption. but if you not just trying to experiment its good avoiding these values.

Blog at WordPress.com.