Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
<script type="hbars" id="whatIHave">
{{#each this}}
  key: {{@index}}
  {{#each that}}
    key1: {{@key}}
  {{/each}}
{{/each}}
</script>
  
<script type="hbars" id="whatIWant">
{{#each this}}
  {{#ifObject this}}
    {{#each that}}
      {{@key}}
    {{/each}}
  {{else}}
    {{@key}}
  {{/ifObject}}
{{/each}}
</script>
  
</body>
</html>
 
var whatIHave = Handlebars.compile($('#whatIHave').text());
var whatIWant = Handlebars.compile($('#whatIWant').text());
var stuff = [{
  there: 'blah',
  that: {
    one: 'bbb',
    two: 'ccc'
  }
}];
Handlebars.registerHelper('ifObject', function(item, options) {
  if(typeof item === "object") {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});
console.log(whatIHave(stuff));
console.log(whatIWant(stuff));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers