0

I'll start with an acknowledgement that there are many similar questions and I've reviewed all of them and tried their suggestions without success. I have a simple array of objects:

data_array = [{'prop1': 64, 'prop2': 'Y', 'prop3':83},
        {'prop1': 62, 'prop2': 'N', 'prop3':105},
        {'prop1': 89, 'prop2': 'Y', 'prop3':75},
        {'prop1': 74, 'prop2': 'N', 'prop3':23}]

I have another array that lists some new names for the properties and the order that I need the properties in

renaming_array = [{'order' : 1, 'oldName' : "prop1", 'newName' : "Condition"},
            {'order' : 2, 'oldName' : "prop2" , 'newName' : "Significance"},
            {'order' : 3, 'oldName' : "prop3" , 'newName' : "Lower Bound"}]

For each object in the data_array, I need to rename the properties and reorder them. I've gone about extracting the keys of each array and ordering them according to the order in renaming_array and that works well (I've done that a variety of ways). But every time I convert that sorted array back into an object that contains the values from the original data_array, the objects get resorted as the originally were. Most forums I've found suggest this method:

const sorted = Object.keys(obj)
  .sort()
  .reduce((accumulator, key) => {
    accumulator[key] = obj[key];

    return accumulator;
  }, {});

but it has the same problem as I described above. I'm open to working with D3 methods, if there are any that would help with this problem. This seems like it shouldn't be this difficult!

Nathaniel Flick
  • 2,902
  • 2
  • 22
  • 31
sreed
  • 65
  • 1
  • 6
  • 1
    Does this answer your question? [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Konrad Aug 29 '22 at 22:35
  • 1
    `the order that I need the properties in` Why would you need properties to be in a specific order? This sounds like an [xy problem](https://xyproblem.info/) to me –  Aug 29 '22 at 22:37

1 Answers1

0

You shouldn't rely on properties being in a certain order. That being said, with modern JS and browsers it should basically work, and it does seem to, for example:

const data_array = [{'prop1': 64, 'prop2': 'Y', 'prop3':83},
        {'prop1': 62, 'prop2': 'N', 'prop3':105},
        {'prop1': 89, 'prop2': 'Y', 'prop3':75},
        {'prop1': 74, 'prop2': 'N', 'prop3':23}];

const renaming_array = [{'order' : 1, 'oldName' : "prop1", 'newName' : "Condition"},
            {'order' : 2, 'oldName' : "prop2" , 'newName' : "Significance"},
            {'order' : 3, 'oldName' : "prop3" , 'newName' : "Lower Bound"}];
            
const output = data_array.map(el => Object.fromEntries(
  Object.entries(el)
    .map(([k, v]) => [renaming_array.find(ra => ra.oldName === k).newName, v])
    .sort(([a], [b]) => 
      renaming_array.find(ra => ra.newName === a).order - renaming_array.find(ra => ra.newName === b).order)
));

console.log(output);
James
  • 20,957
  • 5
  • 26
  • 41