Answer the question
In order to leave comments, you need to log in
How to sort objects inside an array by the number of keys?
For example, there is an array
var Array = [{x : 'x', y : 'y'}, {x : 'x'}, {x : 'x', y : 'y', z : 'z'}];
[ {x : 'x'}, {x : 'x', y : 'y'}, {x : 'x', y : 'y', z : 'z'}]
Answer the question
In order to leave comments, you need to log in
Array = Array.sort(function(a, b) {
return Object.keys(a).length - Object.keys(b).length;
});
var arr = [{x : 'x', y : 'y'}, {x : 'x'}, {x : 'x', y : 'y', z : 'z'}];
arr = arr.sort((a, b) => Object.keys(a).length - Object.keys(b).length)
console.log(arr)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question