Piet
Piet

Reputation: 415

Sort nested objects in Array in Javascript

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

Upvotes: 0

Views: 125

Answers (1)

jfriend00
jfriend00

Reputation: 707328

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

Upvotes: 2

Related Questions