简体   繁体   中英

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: ie, @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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM