adammark/Markup.js

Iterate over an object

miki725 opened this issue · 1 comments

Is it posible to iterate over an object without knowing the object keys?

For example, if I have to use the following object in order to populate a select tag where the key is the value of the <option value="..."> and value is what should be displayed. The following object

"choices": {
   "a" : "A",
   "b" : "B"
}

then should yield the following html:

<select>
   <option value="a">A</option>
   <option value="b">B</option>
</select>

I know I can modify the object and bring the keys in the object but I would prefer to look over it directly if possible.

Thank you.

No. The easiest thing to do would be to convert the object to an array before passing it to the template. Or you could write a custom pipe: https://github.com/adammark/Markup.js#writing-custom-pipes

For example,

Mark.pipes.options = function (obj) {
    var html = "<select>";
    for (var i in obj) {
        html += '<option value="' + i + '">' + obj[i] + '</option>';
    }
    html += "</select>";
    return html;
}

Then do:

{{choices|options}}