<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Javascript snippet: Find the value of a string key in a multidimensional array or object</title>
	<atom:link href="http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/feed/" rel="self" type="application/rss+xml" />
	<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/</link>
	<description>User Experience Design, Web Development, and Internet Marketing</description>
	<lastBuildDate>Sun, 15 Jan 2012 00:45:59 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
	<atom:link rel="hub" href="http://superfeedr.com/hubbub" />
		<item>
		<title>By: Brian Cray</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30921</link>
		<dc:creator>Brian Cray</dc:creator>
		<pubDate>Tue, 15 Dec 2009 17:29:46 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30921</guid>
		<description>Paul: Do a fair amount of browsers support .hasOwnProperty?</description>
		<content:encoded><![CDATA[<p>Paul: Do a fair amount of browsers support .hasOwnProperty?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Grenier</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30916</link>
		<dc:creator>Paul Grenier</dc:creator>
		<pubDate>Tue, 15 Dec 2009 16:51:47 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30916</guid>
		<description>I often have result sets in a JSON object and need to return more than one instance of a key.  So, I wrote something similar that collects values for all instances of the &quot;key.&quot;  I also decided to use toString() to help ensure single-member arrays are handled correctly.  I added the .length test to see if the object is actually an Array (value) instead of another object to be searched.  hasOwnProperty will avoid someone else&#039;s extension of Array or Object.

var myObj = {
	test: 1,
	innerObj: {
		test: [1,2,3],
		innerInnerObj: {
			test: 3
		}
	}
};

String.prototype.findValuesIn = function(obj){
	var arr = [], x, that = this.toString(),
	f = function (o){
		for (x in o) {
			if (typeof o[x] === &quot;object&quot; &amp;&amp; o.hasOwnProperty(x)){
				if (o[x].length) {
					arr.push(o[x]);
				} else {
					f(o[x]);
				}
			} else if (x === that){
				arr.push(o[x]);
			}
		}
	};
	
	f(obj);
	return arr;
};

&quot;test&quot;.findValuesIn(myObj); //[1, [1, 2, 3], 3]</description>
		<content:encoded><![CDATA[<p>I often have result sets in a JSON object and need to return more than one instance of a key.  So, I wrote something similar that collects values for all instances of the &#8220;key.&#8221;  I also decided to use toString() to help ensure single-member arrays are handled correctly.  I added the .length test to see if the object is actually an Array (value) instead of another object to be searched.  hasOwnProperty will avoid someone else&#8217;s extension of Array or Object.</p>
<p>var myObj = {<br />
	test: 1,<br />
	innerObj: {<br />
		test: [1,2,3],<br />
		innerInnerObj: {<br />
			test: 3<br />
		}<br />
	}<br />
};</p>
<p>String.prototype.findValuesIn = function(obj){<br />
	var arr = [], x, that = this.toString(),<br />
	f = function (o){<br />
		for (x in o) {<br />
			if (typeof o[x] === &#8220;object&#8221; &amp;&amp; o.hasOwnProperty(x)){<br />
				if (o[x].length) {<br />
					arr.push(o[x]);<br />
				} else {<br />
					f(o[x]);<br />
				}<br />
			} else if (x === that){<br />
				arr.push(o[x]);<br />
			}<br />
		}<br />
	};</p>
<p>	f(obj);<br />
	return arr;<br />
};</p>
<p>&#8220;test&#8221;.findValuesIn(myObj); //[1, [1, 2, 3], 3]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javascript snippet: Find the value of a string key in a multidimensional array or object &#124; Design Newz</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30632</link>
		<dc:creator>Javascript snippet: Find the value of a string key in a multidimensional array or object &#124; Design Newz</dc:creator>
		<pubDate>Wed, 09 Dec 2009 23:02:02 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30632</guid>
		<description>[...] Javascript snippet: Find the value of a string key in a multidimensional array or object [...]</description>
		<content:encoded><![CDATA[<p>[...] Javascript snippet: Find the value of a string key in a multidimensional array or object [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jayaprakash</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30538</link>
		<dc:creator>Jayaprakash</dc:creator>
		<pubDate>Tue, 08 Dec 2009 07:14:20 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30538</guid>
		<description>Hi Brian,

The feature we can achieve with this is quite good, but unfortunately there are some things that I thought need to be considered:

(1) Extending a built-in object - In this code you have extended a built-in JavaScript object without checking whether the object support its own findIn method. I am sure that at the moment String object does not support a findIn method. But personally I feel that it would be a lot better if we can avoid extending the built-in object, if that is possible.

(2) Works on String - I don&#039;t know whether this is a personal feeling towards the approach using String. As a developer I feel this approach would be far better if this works directly on a JavaScript object instead of a string.

Taffy DB ( http://taffydb.com/ ) is one such framework through which one can manipulate JS literal objects (simple as well as complex one) quickly.</description>
		<content:encoded><![CDATA[<p>Hi Brian,</p>
<p>The feature we can achieve with this is quite good, but unfortunately there are some things that I thought need to be considered:</p>
<p>(1) Extending a built-in object &#8211; In this code you have extended a built-in JavaScript object without checking whether the object support its own findIn method. I am sure that at the moment String object does not support a findIn method. But personally I feel that it would be a lot better if we can avoid extending the built-in object, if that is possible.</p>
<p>(2) Works on String &#8211; I don&#8217;t know whether this is a personal feeling towards the approach using String. As a developer I feel this approach would be far better if this works directly on a JavaScript object instead of a string.</p>
<p>Taffy DB ( <a href="http://taffydb.com/" rel="nofollow">http://taffydb.com/</a> ) is one such framework through which one can manipulate JS literal objects (simple as well as complex one) quickly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Cray</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30498</link>
		<dc:creator>Brian Cray</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:13:10 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30498</guid>
		<description>@Steward, @Tony: LOL! I was returning different things during some testing stuff and forgot to combine those two afterward. What an obvious oversight. haha!</description>
		<content:encoded><![CDATA[<p>@Steward, @Tony: LOL! I was returning different things during some testing stuff and forgot to combine those two afterward. What an obvious oversight. haha!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tony</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30497</link>
		<dc:creator>tony</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:02:44 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30497</guid>
		<description>somebody posted the same thing before, my bad.

Note to self: refresh page before posting.</description>
		<content:encoded><![CDATA[<p>somebody posted the same thing before, my bad.</p>
<p>Note to self: refresh page before posting.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tony</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30496</link>
		<dc:creator>tony</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:01:54 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30496</guid>
		<description>why this redundancy in the code:

if(typeof multi == &#039;object&#039;)
{
    return multi[val];
}
else if(typeof multi == &#039;array&#039;)
{
    return multi[val];
}

why not just:

return multi[val];

or unless there is some javascript object/array functionality that prevents it.</description>
		<content:encoded><![CDATA[<p>why this redundancy in the code:</p>
<p>if(typeof multi == &#8216;object&#8217;)<br />
{<br />
    return multi[val];<br />
}<br />
else if(typeof multi == &#8216;array&#8217;)<br />
{<br />
    return multi[val];<br />
}</p>
<p>why not just:</p>
<p>return multi[val];</p>
<p>or unless there is some javascript object/array functionality that prevents it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steward</title>
		<link>http://briancray.com/2009/12/07/javascript-find-value-string-key-multidimensional-array-object/#comment-30495</link>
		<dc:creator>Steward</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:00:15 +0000</pubDate>
		<guid isPermaLink="false">http://briancray.com/?p=1452#comment-30495</guid>
		<description>if(typeof multi == &#039;object&#039;) {
	return multi[val]; ///???
} else if(typeof multi == &#039;array&#039;) {
	return multi[val]; ///???
}
Not the same things?</description>
		<content:encoded><![CDATA[<p>if(typeof multi == &#8216;object&#8217;) {<br />
	return multi[val]; ///???<br />
} else if(typeof multi == &#8216;array&#8217;) {<br />
	return multi[val]; ///???<br />
}<br />
Not the same things?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

