0

I receive an object from MongoDB request. Below is a snippet of it:

{
    "Kost": "Kost1",
    "Name": "Name1",
    "inventar": [
            {
                "data": "A",
                "name": "thefirst",
                "ean": "802.0165.813",
            },
            {
                "ean": "802.6725.277",
                "name": "thesecond",
                "data": "B",
            },
            {
                "ean": "570.6761.483",
                "name": "thethird",
                "data": "C",
            },
            {
                "ean": "570.6764.519",
                "name": "thefourth",
                "data": "D",
            }
        ]
    }

Later, I will create a table in Jade with this code:

table(border='1', cellspacing='3', cellpadding='4')
   tr
    th(align='center') ean
    th(align='center') name
    th(align='center') data
   each obj in inventar
    tr
    each val in obj
      td=  val

The problem is, that the objects in the Array "inventar" are not sorted. The table has a wrong structure. The current output of the table looks like:

|ean         |   name                 | data
--------------------------------------------
|802.0165.813|  thefirst              |     A
|B           |  thesecond             | 802.6725.277
|C           |  thethird              | 570.6761.483
|D           |  thefourth             | 570.6764.519

The first column must be the ean, second the name and third the data. Only the first row is correct. I think its luck.

Its possible to sort the objects in the Array ("inventar") before iterating over it, to get the right structure? I read somewhere that it is not possible to sort directly in mongoose.

thanks in advance

Piet
  • 395
  • 2
  • 7
  • 17

1 Answers1

2

It appears you are asking about the property order in the object. In ES5 and earlier, properties have NO deterministic order by specification. There are some implementations that will maintain the order the properties were created in, but that was not guaranteed.

In ES6, the spec has been changed to say that properties will remain in the order they are created. But, there is no mechanism for reordering properties on an existing object. If you want to change the order, the work-around would be to create a new object, copy the properties over in the desired order and then replace the original object with the new one.

All that said, normal coding should not care what order the properties are in. You refer to a property on an object as in x.inventar[0].data and it should not matter whether data is the first or last property when you dump the object contents.


Given what you are showing in your sample table, it appears that some piece of code is grabbing the first property and putting it in the first column. That is the wrong way to build the table. Instead, it should grab a specific property name and then the order of the properties on the object simply will not matter. So, I think what you need to do is to fix your jade definition to refer to specific property names, not to just take them in order.

I don't know Jade very well myself, but I think you can do something like this:

table(border='1', cellspacing='3', cellpadding='4')
   tr
    th(align='center') ean
    th(align='center') name
    th(align='center') data
   each obj in inventar
    tr
      //edited syntax
      td= obj.ean
      td= obj.name
      td= obj.data
Piet
  • 395
  • 2
  • 7
  • 17
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • the Order is important, because the dynamic creation of the table depends on the order of the objects – Piet Aug 17 '15 at 20:29
  • 1
    @Piet - that's only because your jade is not referencing specific properties. You've already hard coded the property names in the header column of the table - you can just change your jade to reference the actual property name you want for each column rather than just taking them in order. This is better to fix with Jade, than it is to rebuild the Javascript objects. I, myself, don't know Jade, but am looking now as to how to refer to specific property names. – jfriend00 Aug 17 '15 at 20:31
  • @Piet - see this answer [http://stackoverflow.com/questions/12794078/using-jade-to-iterate-json](http://stackoverflow.com/questions/24316772/iterate-over-a-json-object-in-jade) and [this answer](http://stackoverflow.com/questions/24316772/iterate-over-a-json-object-in-jade) for how to reference specific property names when iterating an array of objects in Jade. – jfriend00 Aug 17 '15 at 20:34
  • its not hardcorded in the header, its only a normal text. the reference happens in the 'each' part. thank you for your help till now. what I need to change? – Piet Aug 17 '15 at 20:35
  • @Piet - I just started learning Jade myself 10 minutes ago, but based on the two above questions/answers I referenced, I added an idea for how to fix the Jade to the end of my answer. – jfriend00 Aug 17 '15 at 20:37