0

I've seen this question answered before, but not quite what I was looking for I have an object like:

const obj = {
    'a': {
        name: 'someName',
        dimension1: '19',
        dimension2: '8',
        ...
    }
    'b': {
        name: 'someName',
        dimension1: '19',
        dimension2: '9',
        ...
    }
    'c': {
        name: 'someName',
        dimension1: '19',
        dimension2: '8.5',
        ...
    }
    'd': {
        name: 'someName',
        dimension1: '19',
        dimension2: '8.5',
        ...
    }
}

I want to reorder the object like so:

Order it by a string dimension1xdimension2

So for obj[a] would be something like '19x8'

The corect ordered keys for the obj would be: a, c, d, b Because: '19x8, '19x8.5', '19x8.5', '19x.9'

How can I achieve this ?

Roland
  • 663
  • 1
  • 9
  • 13
  • 1
    Although object properties *do* have an order now, using it is almost never a good idea. If you want order, use an ordered structure like an array, or something like a `Map` where the order is simpler (purely based on when the key/value was added). Object property order is complex and depends on A) the order the properties were added, B) whether the property name is a string or a Symbol, C) the *value* of the string (sometimes), D) whether the property is "own" or inherited. – T.J. Crowder Nov 24 '21 at 10:42

0 Answers0