We have an array of objects with a certain number of properties in them, we need to sort them by ascending from smaller to larger

var objectArray = [{a : 'a', b : 'b'}, {a : 'a'}, {a : 'a', b : 'b', c : 'c'}]; //вот что по идее должно получится на выходе // [{a : 'a'}, {a : 'a', b : 'b'}, {a : 'a', b : 'b', c : 'c'}] 
  • Any good ideas about this? - GrumpyCoder

1 answer 1

It is logical to use the sort () method, sorted by the dimension of objects

 var objectArray = [{a : 'a', b : 'b'}, {a : 'a'}, {a : 'a', b : 'b', c : 'c'}]; objectArray.sort(function(a, b) { return Object.keys(a).length - Object.keys(b).length; }); console.log(objectArray); 

  • I did not even think that everything would be so simple. I started to reinvent the wheel and, by the way, yes, I used sort () in it. Thanks :) - GrumpyCoder