1

I have an array of objects over which I have to iterate, but I don't know the property name of object.

{{# each }}

{{??}} {{/each}}

4
  • 1
    Why don't you know the name of the keys in the object? Commented May 5, 2017 at 12:19
  • I guess you at least know where do you get it from? Commented May 5, 2017 at 12:26
  • @Sean My keys are being generated dynamically. Commented May 5, 2017 at 12:52
  • @RamilMuratov:Yes, I know the collecting i am getting from. Commented May 5, 2017 at 12:52

1 Answer 1

1

Break it down into two #eachs. The outer #each loops over the array of objects. The inner #each calls a template helper that returns an array of objects with label and value of each property of that object.

The template:

{{#each arrayOfObjects}}
  {{#each getAllFields}}
     <div class="item">
        {{this.label}} <span class="field-value">{{this.value}}</span>
     </div>
  {{/each}}
{{/each}}

The helper:

getAllFields: function() {
    let fields = [];
    const unknownObject = this;

    _.each(Object.keys(unknownObject), function(theKey) {
        fields.push({label: theKey, value: unknownObject[theKey] });
    });

    fields = _.sortBy(fields, 'label');
    return fields;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.