Search code examples
arrayspug

How to extract the value of each first item in Pug array


I'm trying to get the first item in this array using Pug.

- val newx = {'O1_Carmine':'#E54A34', 'O2_Lipstick':'#D04735'}

each val,index in newx
    .span2(style={ background: val, color: 'black', height: '3em'} class='pad1')
        p index[0]

This renders

<div class="span2 pad1" style="background:#E54A34;color:black;height:3em;"><p>index[0]</p></div>
<div class="span2 pad1" style="background:#D04735;color:black;height:3em;"><p>index[0]</p></div>

but I'm trying to get;

<div class="span2 pad1" style="background:#E54A34;color:black;height:3em;"><p>O1_Carmine</p></div>
<div class="span2 pad1" style="background:#D04735;color:black;height:3em;"><p>O2_Lipstick</p></div>

Any tips?


Solution

  • Try this

    each val,index in newx
        .span2(style={ background: val, color: 'black', height: '3em'} class='pad1')
            p= index
    

    Note the = after the p, and no need for [0] (and that first line should read - var, not - val)