0

I'm looking for a way to reverse alphabetical the object keys. Currently, it stores correctly in alphabetical order, what would be the best approach to ensure it reverses the keys correctly? I see there are a lot of options for arrays to reverse loop but not seeing anything for objects. Is it possible to 'unshift' the keys that the reduce exports?

var alph = Object.keys(feedData)
    .sort()
    .reduce(function (acc, key) { 
        acc[key] = feedData[key];
        return acc;
    }, {});

    console.log(alph);


var config = {
    "size": [{
      "F2-1_largerLegal-input": "",
      "F3-1_copy-font": "",
      "Frame-Three": "",
      "_assetKey": "",
      "F3-2_copy-fontSize": "",
      "_jvxRtnWeight": "",
      "CTA-1_copy-fontSize": "",
      "F2-1_copy-font": "",
      "EF-1_largerLegal-fontSize": "",
      "reportingName": "",
      "EF-2_copy-fontSize": "",
      "EF-2_copy-font": "",
      "F2-3_copy-fontSize": "",
      "Frame-One": "",
      "assetVarient": "",
      "F3-1_largerLegal-fontSize": "",
      "EF-1_product-src": "",
      "F3-1_frameLegal-input": "",
      "F3-1_frameLegal-fontSize": "",
      "F3-1_copy-input": "",
      "F2-1_copy-input": "",
      "EF-3_copy-fontSize": "",
      "F2-1_frameLegal-input": "",
      "F3-3_copy-fontSize": "",
      "F3-2_bg-src": "",
      "EF-1_copy-input": "",
      "F2-1_frameLegal-fontSize": "",
      "F2-2_copy-input": "",
      "F2-3_copy-input": "",
      "F3-3_copy-input": "",
      "EF-1_largerLegal-input": "",
      "EF-1_copy-font": "",
      "EF-1_frameLegal-fontSize": "",
      "F3-3_copy-font": "",
      "RO-1_legal-input": "",
      "EF-1_frameLegal-input": "",
      "F2-3_copy-font": "",
      "EF-2_copy-input": "",
      "End-Frame": "",
      "F3-2_copy-input": "",
      "F2-1_copy-fontSize": "",
      "F2-1_product-src": "",
      "F2-2_copy-font": "",
      "rotation": "",
      "F3-2_copy-font": "",
      "exitUrl": "",
      "EF-3_copy-input": "",
      "CTA-1_copy-input": "",
      "F3-1_copy-fontSize": "",
      "EF-1_copy-fontSize": "",
      "Frame-Two": "",
      "F3-1_bg-src": "",
      "EF-3_copy-font": "",
      "weighTag": "",
      "F3-1_largerLegal-input": "",
      "F2-2_copy-fontSize": "",
      "dimensions": "",
      "F2-1_largerLegal-fontSize": ""
    }]
  };
  • 4
    Bigger question, why does the order matter? You cannot guarentee the order of keys on objects. And as they are hashed in some manner for quick lookup, the order doesn't matter. If you need to impose/guarentee an order, then you should use an array of either each piece, or an array of the keys that you have sorted, that you then later iterate over when you need them in a particular order. – Taplar May 27 '20 at 20:48
  • 1
    Use `.sort().reverse()` to sort items in descending order. – Ahmed Hammad May 27 '20 at 20:49
  • 2
    Does this answer your question? [Sorting strings in descending order in Javascript (Most efficiently)?](https://stackoverflow.com/questions/52030110/sorting-strings-in-descending-order-in-javascript-most-efficiently), or [How can I reverse an array in JavaScript without using libraries?](https://stackoverflow.com/questions/10168034/how-can-i-reverse-an-array-in-javascript-without-using-libraries) – Roqux May 27 '20 at 20:51
  • did you want to print the object with the keys reversed instead? – richytong May 29 '20 at 00:31

1 Answers1

0

You cannot ensure the order of keys added with a normal object, as stated in the comments.

You could use an array of sorted Keys, as stated in the comments, or we could use a Map object, and this can be iterated over natively, in the order of key addition.

Here is an example:

const initObj = {
  "a": 1,
  "c": 3,
  "b": 2,
  "e": 5,
  "d": 4
}

//Option 1, created a sorted array of keys:
const sortedKeys = Object.keys(initObj).sort((a,b) => b.localeCompare(a));
console.log(sortedKeys);

//now loop over your initObj using our sortedKeys:
console.log("using sortedKeys:");
for (let key of sortedKeys){
  console.log(key, initObj[key]);
}

//Option 2, use a Map object:
const mapObj = new Map(Object.entries(initObj).sort(([key_a, a],[key_b, b]) => key_b.localeCompare(key_a)));

//now loop over your Map Object directly:
console.log("using mapObj:");
for (let [key, val] of mapObj){
  console.log(key, val);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alex L
  • 4,168
  • 1
  • 9
  • 24