Q:

Get value by name with handlebars

I have a huge table with too many columns to fit on-screen.   I want the user to be able to select which columns to show using checkboxes.   The checkboxes part is easy but I'm not sure how to show the table using handlebars.   Is there some helper I could make that would help?   I'm using Meteor.js. 
{stats: 
    {symbol: 'A', stat1: 5, stat2, 24.3, stat3: 293, stat4: 3},
    {symbol: 'B', stat1: 4, stat2, 24.3, stat3: 293, stat4: 3},
    {symbol: 'C', stat1: 2, stat2, 24.3, stat3: 293, stat4: 3}
}

{columns:
    {key: 'stat1', name: 'Stat 1'},
    {key: 'stat2', name: 'Stat 2'},
    {key: 'stat3', name: 'Stat 3'},
    {key: 'stat4', name: 'Stat 4'}
}

{currentUser: {columnsToShow: ['stat1', 'stat3']}}

<table>
    {{#each stats}}
        <tr>
            <td>{{symbol}}</td>
            {{#each columns}}
                {{#if inArray key currentUser.columnsToShow}}
                    <td>
                        {{!--  the following is what i'm not sure how to do --}}
                        {{stats[key]}}
                    </td>
                {{/if}
            {{/each}}
        </tr>
    {{/each}}
</table>

inArray is a helper I made that returns true if it finds key in the array or false if it doesn't. 
Here's what I expect the table to look like
A    5    293
B    4    293
C    2    293

EDIT: got it to work
{{#if inArray key currentUser.columnsToShow}}
    <td>
        {{returnArrayValueByKeyName key ../../this}}
    </td>
{{/if}}

Handlebars.registerHelper('returnArrayValueByKeyName', function(keyName, array) {
    return array[keyName];
})

A:

You can change your columns helper to a stats_to_show helper that returns the stats for just the columns that you want:
<table>
    {{#each stats}}
        <tr>
            <td>{{symbol}}</td>
            {{#each stats_to_show}}
                <td>
                  {{this}}
                </td>
            {{/each}}
        </tr>
    {{/each}}
</table>

The stats_to_show helper would look like this
Template.stats_table.stats_to_show = function() {
    result = [];
    for (column in currentUser.columnsToShow) {
        // We're using the fact that a single element of stats is bound to
        // `this` when this helper is called. 
        result.push(this[column];
    }
    return result;
}

Sorry if my Javascript syntax is off.  I've been exclusively using Coffeescript lately.