Skip to content

Instantly share code, notes, and snippets.

View styopdev's full-sized avatar
:octocat:
Working from home

Stepan V styopdev

:octocat:
Working from home
View GitHub Profile
@styopdev
styopdev / JSON.stringify-replacer-sort-keys.js
Created October 16, 2023 12:37 — forked from davidfurlong/JSON.stringify-replacer-sort-keys.js
JSON.stringify replacer function for having object keys sorted in output (supports deeply nested objects)
// Spec http://www.ecma-international.org/ecma-262/6.0/#sec-json.stringify
const replacer = (key, value) =>
value instanceof Object && !(value instanceof Array) ?
Object.keys(value)
.sort()
.reduce((sorted, key) => {
sorted[key] = value[key];
return sorted
}, {}) :
value;