That stack overflow thread is very informative.
If I understand correctly since ES6 there is an order (The traversal order of object properties in ES6) and on the stack overflow are numerous ways to re-sort the keys.
That stack overflow thread is very informative.
If I understand correctly since ES6 there is an order (The traversal order of object properties in ES6) and on the stack overflow are numerous ways to re-sort the keys.
So JavaScript continues to "improve" itself to the point of no return
.
I used the Stack Overflow replies to come up with this;
/**************************************************************************************************
*
* Description. Create a new Object, by adding the properties of the old Object in order of the Array of 'keys'
* If required, add any remaining properties of old Object to new Object
*
* @param {Object} object The object to operate on
* @param {Array} keysInOrder An Array of the keys required, in order to be added
* @param {boolean} addAll True if all properties of old Object to be added to new Object, false if only keys. Default is true
* in Array to be added
*
* @return {Object} An Object with the properties in 'key' order of the provided Array, plus any other properties in
* original order
*
* @Example let orderedObject = orderObjectProperties({c: 1, a: 2, b: 1}, ['a', 'b'], false) becomes {a: 2, b: 1}
*/
function orderObjectProperties(object, keysInOrder, addAll = true) {
let newObject = {}
// Add the new Object properties in order of 'keys' in Array
for (const key of keysInOrder) {
if (Object.keys(object).includes(key) && !Object.keys(newObject).includes(key))
newObject[key] = object[key]
}
// Add any old Object properties not yet added, if required
if (addAll) {
for (const key of Object.keys(object)) {
if (!Object.keys(newObject).includes(key))
newObject[key] = object[key]
}
}
return newObject
} // End Function orderObjectProperties()
Perhaps you should add it to the flows, so its easier to find.