<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Pruning JavaScript array.</title>
	<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array/</link>
	<description>Comments on Ask MetaFilter post Pruning JavaScript array.</description>
	<pubDate>Tue, 15 Aug 2006 09:08:52 -0800</pubDate>
	<lastBuildDate>Tue, 15 Aug 2006 09:08:52 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Pruning JavaScript array.</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array</link>	
		<description>Multidimensional JavaScript array that needs pruning. &lt;br /&gt;&lt;br /&gt; I have a multidimensional arary :&lt;br&gt;
&lt;br&gt;
&lt;i&gt;myArray =[[1, 23, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [2, 389, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [3, 42, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [4, 2, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [5, 17, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [6, 45, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [7, 95, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [8, 101, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [9, 112, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [10, 902, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [11, 9, &apos;Text&apos;, &apos;Text&apos;], &lt;br&gt;
		 [12, 22, &apos;Text&apos;, &apos;Text&apos;]];&lt;br&gt;
&lt;/i&gt;&lt;br&gt;
I also have a single-dimensional array.&lt;br&gt;
&lt;br&gt;
&lt;i&gt;myOtherArary = [ 22, 45, 2, 11 ];&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I wish to delete &apos;rows&apos; from myArray where the second values i.e. &lt;i&gt;(elements myArray[0][1], myArray[1][1], myArray[1][1].....myArray[11][1])&lt;/i&gt; are &lt;b&gt;not&lt;/b&gt; in myOtherArray.&lt;br&gt;
&lt;br&gt;
For the sample values provided the final contents of myArray should be:&lt;br&gt;
&lt;i&gt;&lt;br&gt;
myArray =[[4, 2, &apos;Text1&apos;, &apos;Text2&apos;], &lt;br&gt;
		  [6, 45, &apos;Text1&apos;, &apos;Text2&apos;], &lt;br&gt;
		  [12, 22, &apos;Text1&apos;, &apos;Text2&apos;]];&lt;br&gt;
&lt;br&gt;
&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
How can I achieve this? Thanks in advance.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2006:site.44451</guid>
		<pubDate>Tue, 15 Aug 2006 08:34:12 -0800</pubDate>
		<dc:creator>kenaman</dc:creator>
		
			<category>Programming</category>
		
			<category>JavaScript</category>
		
	</item> <item>
		<title>By: grumblebee</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681489</link>	
		<description>var element;&lt;br&gt;
var tempArray;&lt;br&gt;
var found;&lt;br&gt;
&lt;br&gt;
for (i = 0; i &amp;lt; myArray.length; i++)&lt;br&gt;
{&lt;br&gt;
element = myArray[i][1];&lt;br&gt;
found = false;&lt;br&gt;
for (j = 0; j &amp;lt; myOtherArray.length; j++)&lt;br&gt;
{&lt;br&gt;
if (element = myOtherArray[j])&lt;br&gt;
{&lt;br&gt;
found = true;&lt;br&gt;
break;&lt;br&gt;
}&lt;br&gt;
if (found) tempArray.push(myArray[i]);&lt;br&gt;
}&lt;br&gt;
myArray = tempArray.slice();&lt;br&gt;
}</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681489</guid>
		<pubDate>Tue, 15 Aug 2006 09:08:52 -0800</pubDate>
		<dc:creator>grumblebee</dc:creator>
	</item><item>
		<title>By: utsutsu</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681500</link>	
		<description>As Grumblebee&apos;s example illustrates, you don&apos;t remove the rows from the array, but rather you build a new array with the rows you want.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681500</guid>
		<pubDate>Tue, 15 Aug 2006 09:16:39 -0800</pubDate>
		<dc:creator>utsutsu</dc:creator>
	</item><item>
		<title>By: orthogonality</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681516</link>	
		<description>&lt;b&gt;utsutsu&lt;/b&gt; &lt;a href=&apos;http://ask.metafilter.com/mefi/44451#681500&apos;&gt;writes&lt;/a&gt;  &lt;em&gt;&quot;you don&apos;t remove the rows from the array, but rather you build a new array with the rows you want.&quot;&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
It&apos;s kinda the opposite of Rumsfeld: You don&apos;t go to code with the arrays you have, you go to code with the arrays you want.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681516</guid>
		<pubDate>Tue, 15 Aug 2006 09:24:45 -0800</pubDate>
		<dc:creator>orthogonality</dc:creator>
	</item><item>
		<title>By: koenie</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681524</link>	
		<description>&lt;pre&gt;&lt;br&gt;
function prune_rows(array, condition) {&lt;br&gt;
	var result = [];&lt;br&gt;
	for (var i = 0; i &lt; array.length; ++i) {br&gt;
		if (!condition(array, i)) {&lt;br&gt;
			result.push(array[i]);&lt;br&gt;
		}&lt;br&gt;
	}&lt;br&gt;
	return result;&lt;br&gt;
}&lt;br&gt;
		&lt;br&gt;
myArray = [&lt;br&gt;
	[1,  23,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[2,  389, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[3,  42,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[4,  2,   &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[5,  17,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[6,  45,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[7,  95,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[8,  101, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[9,  112, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[10, 902, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[11, 9,   &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[12, 22,  &apos;Text&apos;, &apos;Text&apos;]&lt;br&gt;
];&lt;br&gt;
&lt;br&gt;
function to_be_pruned(array, row_index) {&lt;br&gt;
	var myOtherArray = [22, 45, 2, 11];&lt;br&gt;
	return myOtherArray.indexOf(array[row_index][1]) == -1;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
alert(prune_rows(myArray, to_be_pruned));&lt;br&gt;
&lt;/&gt;&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681524</guid>
		<pubDate>Tue, 15 Aug 2006 09:32:28 -0800</pubDate>
		<dc:creator>koenie</dc:creator>
	</item><item>
		<title>By: kenaman</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681561</link>	
		<description>koenie - &lt;br&gt;
&lt;br&gt;
&lt;i&gt;return myOtherArray.indexOf(array[row_index][1]) == -1;&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
throws error :Object doesnt support this property or method.&lt;br&gt;
&lt;br&gt;
myOtherArray and arry both contain what one would expect them to and row_index = 0.&lt;br&gt;
&lt;br&gt;
Any ideas?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681561</guid>
		<pubDate>Tue, 15 Aug 2006 09:52:26 -0800</pubDate>
		<dc:creator>kenaman</dc:creator>
	</item><item>
		<title>By: kenaman</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681598</link>	
		<description>koenie - googled it and see that it is not supported across all browsers. Will figure it out myself. Cheers. Anyway if anyone else wants to come up with a clever way to skin an array, please do.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681598</guid>
		<pubDate>Tue, 15 Aug 2006 10:22:02 -0800</pubDate>
		<dc:creator>kenaman</dc:creator>
	</item><item>
		<title>By: null terminated</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681845</link>	
		<description>&lt;pre&gt;&lt;br&gt;
function prune(a, deleteKeys) {&lt;br&gt;
    var keys = &apos;,&apos;+deleteKeys.join(&apos;,&apos;)+&apos;,&apos;;&lt;br&gt;
    var len = a.length;&lt;br&gt;
    for(var i=0;i&lt;len ;i++)br&gt;
        if(keys.indexOf(&apos;,&apos;+a[i][1]+&apos;,&apos;) == -1)&lt;br&gt;
            a.push(a[i]);&lt;br&gt;
    a.splice(0,len);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
		&lt;br&gt;
&lt;br&gt;
myArray = [&lt;br&gt;
	[1,  23,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[2,  389, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[3,  42,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[4,  2,   &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[5,  17,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[6,  45,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[7,  95,  &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[10, 902, &apos;Text&apos;, &apos;Text&apos;],&lt;br&gt;
	[12, 22,  &apos;Text&apos;, &apos;Text&apos;]&lt;br&gt;
];&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
var delArray = [22, 45, 2, 11];&lt;br&gt;
&lt;br&gt;
prune(myArray, delArray);&lt;br&gt;
&lt;br&gt;
&lt;/len&gt;&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
myArray is your new array.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681845</guid>
		<pubDate>Tue, 15 Aug 2006 13:34:35 -0800</pubDate>
		<dc:creator>null terminated</dc:creator>
	</item><item>
		<title>By: null terminated</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681850</link>	
		<description>That didn&apos;t work too well. Here&apos;s the prune function:&lt;br&gt;
&lt;br&gt;
function prune(a, deleteKeys) {&lt;br&gt;
    var keys = &apos;,&apos;+deleteKeys.join(&apos;,&apos;)+&apos;,&apos;;&lt;br&gt;
    var len = a.length;&lt;br&gt;
    for(var i=0;i&amp;lt;len;i++)&lt;br&gt;
        if(keys.indexOf(&apos;,&apos;+a[i][1]+&apos;,&apos;) == -1)&lt;br&gt;
            a.push(a[i]);&lt;br&gt;
    a.splice(0,len);&lt;br&gt;
}</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681850</guid>
		<pubDate>Tue, 15 Aug 2006 13:37:49 -0800</pubDate>
		<dc:creator>null terminated</dc:creator>
	</item><item>
		<title>By: null terminated</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681851</link>	
		<description>looks like &amp;amp;nbsp;s aren&apos;t being converted.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681851</guid>
		<pubDate>Tue, 15 Aug 2006 13:38:16 -0800</pubDate>
		<dc:creator>null terminated</dc:creator>
	</item><item>
		<title>By: null terminated</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681860</link>	
		<description>argh, my answer was backwards. This line:&lt;br&gt;
&lt;br&gt;
if(keys.indexOf(&apos;,&apos;+a[i][1]+&apos;,&apos;) == -1)&lt;br&gt;
&lt;br&gt;
should be&lt;br&gt;
&lt;br&gt;
if(keys.indexOf(&apos;,&apos;+a[i][1]+&apos;,&apos;) != -1)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681860</guid>
		<pubDate>Tue, 15 Aug 2006 13:45:38 -0800</pubDate>
		<dc:creator>null terminated</dc:creator>
	</item><item>
		<title>By: grumblebee</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681938</link>	
		<description> kenaman, did my method work for you?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681938</guid>
		<pubDate>Tue, 15 Aug 2006 14:58:04 -0800</pubDate>
		<dc:creator>grumblebee</dc:creator>
	</item><item>
		<title>By: grumblebee</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#681945</link>	
		<description>(We need to lobby Matt to give us a way to include code examples, complete with indenting and character parsing. I posted &lt;a href=&quot;null&quot;&gt;a MeTa thread&lt;/a&gt; about it, and Matt agreed it was a good idead, but I think it&apos;s on the back burner.)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-681945</guid>
		<pubDate>Tue, 15 Aug 2006 15:03:39 -0800</pubDate>
		<dc:creator>grumblebee</dc:creator>
	</item><item>
		<title>By: kenaman</title>
		<link>http://ask.metafilter.com/44451/Pruning-JavaScript-array#682291</link>	
		<description>Thanks for the answers guys. All worked when adapted. Sorry for replying late but I am in eurpoe working late and had to go home.  The help was appreciated. &lt;br&gt;
&lt;br&gt;
grumblebee I second the need for facilitating code examples. Formatting is a nightmare.&lt;br&gt;
&lt;br&gt;
Anyway thanks again.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.44451-682291</guid>
		<pubDate>Wed, 16 Aug 2006 00:40:18 -0800</pubDate>
		<dc:creator>kenaman</dc:creator>
	</item>
	</channel>
</rss>
