Skip to content

Instantly share code, notes, and snippets.

@davidfurlong
Last active May 5, 2024 06:00
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidfurlong/463a83a33b70a3b6618e97ec9679e490 to your computer and use it in GitHub Desktop.
Save davidfurlong/463a83a33b70a3b6618e97ec9679e490 to your computer and use it in GitHub Desktop.
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;
// Usage
// JSON.stringify({c: 1, a: { d: 0, c: 1, e: {a: 0, 1: 4}}}, replacer);
@peternann
Copy link

This is quite clever. Touche.

@congzhou09
Copy link

Ingenious!

@mepanko91
Copy link

Excellent!
Now my getUniqueObjects function works correctly.

function getUniqueObjects(arr) {
  const replacer = (key, value) =>
    value && typeof value === "object" && !Array.isArray(value)
      ? Object.keys(value)
          .sort()
          .reduce((sorted, key) => {
            sorted[key] = value[key];
            return sorted;
          }, {})
      : value;
  return arr.filter(
    (v, i, a) =>
      a.findIndex(
        (v2) => JSON.stringify(v2, replacer) === JSON.stringify(v, replacer)
      ) === i
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment