Gravatar for jpdery@coveo.com

Question by jpdery, May 8, 2014 3:34 PM

How to display all JsSearfch raw fields

Silly-simple but I can't get it to work:

            <table class="CoveoFieldTable">
            < % 
                _.each(raw, function(fld) {
            % >                                
              < tr data-field="< %=fld %>" data-caption="< %=raw[fld]%>" / >
            < % 
                });
            % >                        
            </table>

…fld only contains the value. How to iterate to get the field name and the value out of result.raw ?

2 Replies
Gravatar for apare@coveo.com

Answer by Alexandre Paré, May 8, 2014 3:41 PM

This should work :

<table class="CoveoFieldTable">
  <% _.each(_.keys(raw), function(field) { %>   
    <tr data-field="@<%= field %>" data-caption="<%= field %>" />
  <% }); %>       
</table>

You can also alt+double click on any result to see the debug view of the result.

Gravatar for mlaporte@coveo.com

Comment by Martin Laporte, May 8, 2014 3:44 PM

Alex's answer is superior to mine. Give the guy upvotes.

Gravatar for jpdery@coveo.com

Comment by jpdery, May 8, 2014 3:45 PM

ahhh: alt+doubleclick…

Gravatar for mlaporte@coveo.com

Answer by Martin Laporte, May 8, 2014 3:36 PM

Use for (key in raw) { … } instead of _.each, which enumerates values.

Gravatar for apare@coveo.com

Comment by Alexandre Paré, May 8, 2014 3:47 PM

you can have the key with the function each by using 2 parameters : _.each(myObject, function(value, key) { console.log(value,key); });

Ask a question