The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
let unorderedData = {
real_name: 'Millie Bobby Brown',
character_name: 'Eleven',
series: 'Stranger Things'
};
console.log(JSON.stringify(unorderedData));
const orderedData = {};
Object.keys(unorderedData).sort().forEach(function(key) {
orderedData[key] = unorderedData[key];
});
console.log(JSON.stringify(orderedData));
The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.keys(object1)); // expected output: Array ["a", "b", "c"]let unorderedData = { real_name: 'Millie Bobby Brown', character_name: 'Eleven', series: 'Stranger Things' }; console.log(JSON.stringify(unorderedData)); const orderedData = {}; Object.keys(unorderedData).sort().forEach(function(key) { orderedData[key] = unorderedData[key]; }); console.log(JSON.stringify(orderedData));