<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MLNTN &#187; Flash</title>
	<atom:link href="http://mlntn.com/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://mlntn.com</link>
	<description>Maniacal musings of a pixel perfectionist</description>
	<lastBuildDate>Wed, 19 Jan 2011 19:11:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Iterating through objects, the easy way</title>
		<link>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/</link>
		<comments>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/#comments</comments>
		<pubDate>Thu, 08 May 2008 05:04:24 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://mlntn.com/?p=18</guid>
		<description><![CDATA[Objects in any ECMAScript language (Javascript, Actionscript, Jscript, etc) are very powerful for &#8211; among other things &#8211; storing data.  However, getting some information out of these objects can be a hassle sometimes.
I ran into this issue recently and wrote some cool little handlers for objects.  The following methods can be used to [...]]]></description>
			<content:encoded><![CDATA[<p>Objects in any ECMAScript language (Javascript, Actionscript, Jscript, etc) are very powerful for &#8211; among other things &#8211; storing data.  However, getting some information out of these objects can be a hassle sometimes.</p>
<p>I ran into this issue recently and wrote some cool little handlers for objects.  The following methods can be used to iterate through an array manually.  I wrote them for Actionscript, but they certainly work for Javascript &#8211; although I&#8217;d suggest one small modification (shown later).<span id="more-18"></span></p>
<p>Let&#8217;s get to the code.</p>
<p>In each of the &#8220;for in&#8221; loops below, I make sure to exclude any prototyped methods.  If I don&#8217;t, they&#8217;ll show up in the loop &#8211; which can be pretty bad.</p>
<p>The &#8216;first&#8217; method starts looping through the array, but returns the first item in the object as soon as it gets to it.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">first</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;last&#8217; method loops through the object, storing the current item in a variable.  When it finishes the loop, it returns that variable &#8211; the last item in the object.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">last</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;previous&#8217; method loops through the object looking for the &#8217;subject&#8217; variable.  If it matches the variable with the current item, it returns the previous item &#8211; set after the if statement below (in the previous time through the loop).  If it never matches the variable or the previous item is never set, it will return the last item of the object.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">previous</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> subject <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> l <span style="color: #339933;">!==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>The &#8216;next&#8217; method loops through the object looking for the &#8217;subject&#8217; variable, as well.  However, when it matches the variable it sets a &#8216;found&#8217; flag.  The next time through the loop, the current item will be returned.  If it never matches the variable or the matched item is the last in the object, the first item will be returned.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">next</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>So let&#8217;s do some examples.  My test object has 3 items &#8211; one of which contains an array:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; apple &nbsp;<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'fuji'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'gala'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'red delicious'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; banana <span style="color: #339933;">:</span> <span style="color: #3366CC;">'chiquita'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; orange <span style="color: #339933;">:</span> <span style="color: #3366CC;">'navel'</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
myObject.<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 'apple'</span><br />
myObject.<span style="color: #660066;">last</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #006600; font-style: italic;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">previous</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns 'banana'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'banana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// returns 'orange'</span><br />
myObject.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// returns 'apple'</span></div></div>
<p>Simple as that.</p>
<p>The one change that I would recommend looking into for Javascript is better checking for the &#8220;for in&#8221; loops.</p>
<p>Instead of:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Object.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></div>
<p>The hasOwnProperty property is recommended:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2008/05/08/javascript-iterating-through-objects-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Recursively parse XML into an array</title>
		<link>http://mlntn.com/2007/12/01/flash-recursively-parse-xml-into-an-array/</link>
		<comments>http://mlntn.com/2007/12/01/flash-recursively-parse-xml-into-an-array/#comments</comments>
		<pubDate>Sat, 01 Dec 2007 17:43:55 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://mlntn.com/2007/12/01/flash-recursively-parse-xml-into-an-array/</guid>
		<description><![CDATA[Flash does not make it easy to handle XML.  I&#8217;ve used some really bad code to help me parse arrays until I came up with this.  It doesn&#8217;t handle elements of the same name at the same level, but it can probably be modified to do so.
var myParsedXML = new Array();

var myXML = [...]]]></description>
			<content:encoded><![CDATA[<p>Flash does not make it easy to handle XML.  I&#8217;ve used some really bad code to help me parse arrays until I came up with this.  It doesn&#8217;t handle elements of the same name at the same level, but it can probably be modified to do so.</p>
<pre>var myParsedXML = new Array();

var myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
  if (success) {
    parseXML(myXML);
  }
};

myXML.load("data.xml");
function parseFile(xmlData) {
  var mainXMLElement = xmlData.firstChild.childNodes;
  for (var i = 0; i &lt; mainXMLElement.length; i++) {
    myParsedXML.push(intoArray(mainXMLElement[i]));
  }
}

function intoArray(xml_element){
  var xml_array = new Array();
  if (xml_element.hasChildNodes()) {
    for (var cn = 0; cn &lt; xml_element.childNodes.length; cn++) {
      if (xml_element.childNodes[cn].nodeType == 1) {
        xml_array[xml_element.childNodes[cn].nodeName] = xml_element.childNodes[cn].firstChild;
        if (xml_element.childNodes[cn].attributes) {
          xml_array[xml_element.childNodes[cn].nodeName].attributes = xml_element.childNodes[cn].attributes;
        }
      }
      else {
        xml_array[xml_element.childNodes[cn].nodeName] = intoArray(xml_element.childNodes[cn]);
      }
    }
    return xml_array;
  }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://mlntn.com/2007/12/01/flash-recursively-parse-xml-into-an-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
