From b09dbca3fd9c606540f434602c36a9b5f5ea7e5f Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 18 Mar 2020 10:44:25 +0100 Subject: [PATCH] Fix #917, #926: update ordering of object keys --- HISTORY.md | 2 +- src/js/Node.js | 31 ++++++++----------------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index e477e87..e3a5e6e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,7 +4,7 @@ https://github.com/josdejong/jsoneditor ## not yet published, version 8.6.2 -- Fixed #926: Keep order of properties when updating an object. +- Fixed #917, #926: Keep order of properties when updating an object. - Fixed #928: Custom root name not reflected in path of navigation bar. - Upgraded to `ajv@6.12.0` diff --git a/src/js/Node.js b/src/js/Node.js index 3eed075..6ac27c8 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -1490,31 +1490,16 @@ export class Node { return false } - // TODO: for better efficiency, we could create a property `isDuplicate` on all of the childs - // and keep that up to date. This should make deepEqual about 20% faster. - const props = {} - let propCount = 0 - for (i = 0; i < this.childs.length; i++) { - const child = this.childs[i] - if (!props[child.field]) { - // We can have childs with duplicate field names. - // We take the first, and ignore the others. - props[child.field] = true - propCount++ - - if (!(child.field in json)) { - return false - } - - if (!child.deepEqual(json[child.field])) { - return false - } - } - } - - if (propCount !== Object.keys(json).length) { + // we reckon with the order of the properties too. + const props = Object.keys(json) + if (this.childs.length !== props.length) { return false } + for (i = 0; i < props.length; i++) { + if (this.childs[i].field !== props[i]) { + return false + } + } } else { if (this.value !== json) { return false