A strange situation occurs for the second time. Suppose there is an array of objects of this kind:

var a = [ { 'a' : 1, 'b' : 2 }, { 'a' : 3, 'b' : 4 }, { 'a' : 5, 'b' : 4 } ]; 

In fact, the objects are huge. So I want, for example, to filter an array, taking only objects with b = 4:

 <% var b = a.filter( function( filtered ) { return filtered.b === 4; }); %> 

I get an array of objects b without a zero object of array a:

 [ { 'a' : 3, 'b' : 4 }, { 'a' : 5, 'b' : 4 } ]; 

And here is some kind of anomaly. When I cycle through the elements of an array, I cannot get the properties of the objects:

 <% for( index in b ) { %> <%- b[ index ].b %> <% } %> 

Must get 4 but get emptiness. b [index] .b is of type undefined, but object b [index] is of type object. I am trying to pass an array in other ways:

 <% b.forEach( function( bData ) { %> <%- bData.b %> <% }); %> 

Same. Now I try through .each - ejs swears, there is no such function. Maybe so:

 <% var bLenght = b.length; for( var i = 0; i < bLenght; b++ ) { %> <%- b[i].b %> <% } %> 

No way too.

Why is this happening in ejs template engine? How can I get to the bottom of object properties?


UPDATE

Oh how interesting it turns out ..

 <%- JSON.parse( JSON.stringify( b ) ).b %> 

Get 4. Your comments?

  • in the last version you have an error here: var bLenght and i < bLength - i < bLength
  • so output the whole object and see what kind of object it is - Grundy
  • @Grundy yes, it is listed in the third "listing" ("I get an array of objects b without a zero object of array a") - Matvey Safronov

0