title: I am using Handlebars in my project. I want to show only the first element in an array. How can i do that?
body: <pre><code>   &lt;body&gt;
  
  &lt;!--This is our template. --&gt;
  &lt;!--Data will be inserted in its according place, replacing the brackets.--&gt;
  &lt;script id="t" type="text/x-handlebars"&gt;
      {{#each_when profile "gender" "female"}}
          {{from}} ({{gender}})&lt;br&gt; 
      {{/each_when}}
  &lt;/script&gt;
  
  &lt;!--Your new content will be displayed in here--&gt;
  &lt;div class="content-placeholder"&gt;&lt;/div&gt;
  
  &lt;script&gt;
  var json = {
      "profile": [
          { "gender": "female", "from": "Olivia" },
          { "gender": "female", "from": "Meagen" },
          { "gender": "female",   "from": "Aaron"  },
          { "gender": "female",   "from": "Aaron"  }
      ]
  };
  
  Handlebars.registerHelper('each_when', function(list, k, v, opts) {
      console.log(arguments);
      var i, result = '';
      for(i = 0; i &lt; list.length; ++i)
          if(list[i][k] == v)
              result = result + opts.fn(list[i]);
      return result;
  });
  
  Handlebars.registerHelper('get_length', function (obj) {
   return obj.length;
  }); 
  
  
  var t = Handlebars.compile($('#t').html());
  $('body').append(t(json));
  &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Page Title&lt;/title&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;!--This is our template. --&gt;
  &lt;!--Data will be inserted in its according place, replacing the brackets.--&gt;
  &lt;script id="t" type="text/x-handlebars"&gt;
      {{#each_when profile "gender" "female"}}
          {{from}} ({{gender}})&lt;br&gt; 
      {{/each_when}}
  &lt;/script&gt;
  
  &lt;!--Your new content will be displayed in here--&gt;
  &lt;div class="content-placeholder"&gt;&lt;/div&gt;
  
  &lt;script&gt;
  var json = {
      "profile": [
          { "gender": "female", "from": "Olivia" },
          { "gender": "female", "from": "Meagen" },
          { "gender": "female",   "from": "Aaron"  },
          { "gender": "female",   "from": "Aaron"  }
      ]
  };
  
  Handlebars.registerHelper('each_when', function(list, k, v, opts) {
      console.log(arguments);
      var i, result = '';
      for(i = 0; i &lt; list.length; ++i)
          if(list[i][k] == v)
              result = result + opts.fn(list[i]);
      return result;
  });
  
  Handlebars.registerHelper('get_length', function (obj) {
   return obj.length;
  }); 
  
  
  var t = Handlebars.compile($('#t').html());
  $('body').append(t(json));
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Result:</strong></p>
  
  <ul>
  <li>Olivia (female) </li>
  <li>Meagen (female) </li>
  <li>Aaron (female) </li>
  <li>Aaron (female)</li>
  </ul>
  
  <p><strong>but I want only first one:</strong></p>
  
  <ul>
  <li>Olivia (female) </li>
  </ul>
  
  <p>Also, I am using <strong>{{@index}}</strong> to show the array index, but it is not working for me. Am I missing something?</p>