Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the unique items - Handlebars

My JSON looks like this:

{
  "features": [
    {
      "id": "belly",
      "scenarios": [
        {
          "id": "belly;a-few-cukes",
          "tags": [
            {
              "name": "@tag1"
            }
          ],
          "steps": [
            {
              "name": "I have 42 cukes in my belly"
            },
            {
              "name": "I wait 1 hour"
            },
            {
              "name": "my belly should growls"
            }
          ]
        },
        {
          "id": "belly;a-few-cukes-with-new-test",
          "tags": [
            {
              "name": "@tag2"
            }
          ],
          "steps": [
            {
              "name": "I have 42 cukes in my belly"
            },
            {
              "name": "I wait 1 hour"
            },
            {
              "name": "my belly should growl"
            }
          ]
        }
      ]
    },
    {
      "id": "newbelly",
      "scenarios": [
        {
          "id": "newbelly;a-few-cukes-with-new-feature",
          "tags": [
            {
              "name": "@tag1"
            }
          ],
          "steps": [
            {
              "name": "I have 42 cukes in my belly"
            },
            {
              "name": "I wait 1 hour"
            },
            {
              "name": "my belly should growls"
            }
          ]
        }
      ]
    }
  ]
}

I would like to retrieve all the unique tag names: i.e., @tag1, @tag2. If you notice, the @tag1 is repeated twice.

My template:

{{#getTags features}}
    {{#scenarios}}
        {{#tags}}
            <p>{{name}}</p>
        {{/tags}}
    {{/scenarios}}
{{/getTags}}

Custom Helper that I created so far:

Handlebars.registerHelper('getTags', function(context, block) {
    var ret = "";

    for (var i = 0; i < context.length; i++) {
        ret += block.fn(context[i]);
    };

    return ret;
});

The above custom helper returns all the tags, but I want unique ones.

like image 793
Vimalraj Selvam Avatar asked Jan 01 '26 04:01

Vimalraj Selvam


1 Answers

Something along these lines may work:

Handlebars.registerHelper('getTags', function(context, block) {
var ret = "";
var got = [];

function contains(obj, a) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
return false;
}

for (var i = 0; i < context.length; i++) {
    if (!this.contains(context[i],got)) {
        got.addObject(context[i]);
        ret += block.fn(context[i]);
    }
}

return ret;
});

Code used for testing, all javascript:

var ret = "";
var got = [];
var data = ["tag1", "tag1", "tag2", "tag3"]

function contains(obj, a) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
return false;
}

for (var i = 0; i < data.length; i++) {
    if (!contains(data[i],got)) {
        got.push(data[i]);
        ret += data[i];
        }
}

console.log( ret);
like image 194
millerbr Avatar answered Jan 03 '26 16:01

millerbr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!