How to get distinct values from an array of objects in javascript?
<p>Assuming I have the following:</p>

<pre><code>var array = [{"name":"Joe", "age":17}, {"name":"Bob", "age":17}, {"name":"Carl", "age": 35}]
</code></pre>

<p>What is the best way to be able to get an array of all of the distinct ages such that I get an result array of:</p>

<pre><code>[17, 35]
</code></pre>

<p>Is there some way I could alternatively structure the data or better method such that I would not have to iterate through each array checking the value of "age" and check against another array for its existence, and add it if not?</p>

<p>If there was some way I could just pull out the distinct ages without iterating...</p>

<p>Current inefficent way I would like to improve... If it means that instead of "array" being an array of objects, but a "map" of objects with some unique key (i.e. "1,2,3") that would be okay too. Im just looking for the most performance efficient way.</p>

<p>The following is how I currently do it, but for me, iteration appears to just be crummy for efficiency even though it does work...</p>

<pre><code>var distinct = []
for (var i = 0; i &lt; array.length; i++)
   if (array[i].age not in distinct)
      distinct.push(array[i].age)
</code></pre>
