sort object keys using javascript

 

JavaScript objects are in fact ordered, and the order of their keys/properties can be changed.

Use Object.keys method to sort an object by its keys/properties, alphabetically

 

let obj = {z: 'three', a: 'one', b: 'two'};

let sorted = Object.keys(obj).sort().reduce((accumulator, key) => {
    accumulator[key] = obj[key];

    return accumulator;
  }, {});

console.log(sorted); 

Tags:

Share:

Related posts