 <h3>Step 1 : the target datastructure :</h3> <p>There are two issues with your data structure :</p> <ul> <li><p>You're forgetting to put the names between quotes. <code>John</code> should be <code>"John"</code>. <code>Mary</code> should be <code>"Mary"</code>. etc.</p></li> <li><p>You're trying to use an object for <code>[ "John", "Mary" ]</code> and <code>[ "Paul", "Peter" ]</code>, where you should be using an array.</p></li> </ul> <p>So, the data structure you're looking for, is this :</p> <pre><code>{ 31 : [ "John", "Mary" ], 24 : [ "Paul", "Peter" ] } </code></pre> <hr> <h3>Step 2 : fetching the data from the HTML input :</h3> <p>I suggest you make this small adjustment to your JavaScript code :</p> <pre><code>var source = []; $('span.members a').each(function () { $.ajax({ url: $(this).attr('href'), success: function (result) { source.push({ name : $(result).find('tr td:contains("Username:")').next().text().trim(), age : $(result).find('tr td:contains("Age:")').next().text().trim() }); } }); }); </code></pre> <p>(see also <a href="https://jsfiddle.net/9pwr64ga/2/" rel="nofollow"><strong>this Fiddle</strong></a>)</p> <p>This fetches the data from your HTML input and puts it into a data structure that looks like this :</p> <pre><code>var source = [{ name : "Paul", age : 24 }, { name : "Mary", age : 31 }, { name : "Peter", age : 24 }, { name : "John", age : 31 }]; </code></pre> <hr> <h3>Step 3 : converting to the right data structure :</h3> <p>Once you fetched all the data, you only need to convert your data from the source data structure to the target data structure, which is as simple as this :</p> <pre><code>var convert = function(source) { var output = {}; for(var i = 0; i &lt; source.length; i++) { if(output[source[i].age]) { output[source[i].age].push(source[i].name); } else { output[source[i].age] = [source[i].name]; } } return output; } </code></pre> <p>(see also <a href="https://jsfiddle.net/geyyjj8s/" rel="nofollow"><strong>this Fiddle</strong></a>)</p> <hr> <h3>Putting the pieces of the puzzle together :</h3> <p>You have to wait with executing your <code>convert</code> function until all of your Ajax calls have finished.</p> <p>For example, you could do something like ...</p> <pre><code>if (source.length === numberOfMembers) { target = convert(source); } </code></pre> <p>... right behind the <code>source.push(...)</code> statement.</p> <p>So if you put all the pieces of the puzzle together, you should get something like this :</p> <pre><code>var source = []; var target; var $members = $('span.members a'); var numberOfMembers = $members.size(); var convert = function(source) { var output = {}; for(var i = 0; i &lt; source.length; i++) { if(output[source[i].age]) { output[source[i].age].push(source[i].name); } else { output[source[i].age] = [source[i].name]; } } return output; } $members.each(function () { $.ajax({ url: $(this).attr('href'), success: function (result) { source.push({ name : $(result).find('tr td:contains("Username:")').next().text().trim(), age : $(result).find('tr td:contains("Age:")').next().text().trim() }); if (source.length === numberOfMembers) { target = convert(source); } } }); }); </code></pre>