0

I have the following object...

{0: "#000000", 1: "#FFFFFF", 0.292: "#ff0000", 0.7: "#3498db"}

How can reverse the order of the hex values respective to their keys? This is what I want to achieve...

{0: "#FFFFFF", 0.292: "#3498db", 0.7: "#ff0000", 1: "#000000"}
Rob
  • 14,746
  • 28
  • 47
  • 65
Serks
  • 333
  • 2
  • 21
  • 3
    The order of keys is not guaranteed and frankly, it shouldn't matter. – Scott Marcus Dec 20 '17 at 14:46
  • What are the criteria for rearranging the property values? – Pointy Dec 20 '17 at 14:46
  • Please read question better. – Mihai Alexandru-Ionut Dec 20 '17 at 14:47
  • keys who are integers are sorted first, then comes the rest of the keys by inertation order. that means, key `1` comes directly after key `0`. – Nina Scholz Dec 20 '17 at 14:47
  • 4
    This isn't a question about the enumeration order of keys. – Anthony Mills Dec 20 '17 at 14:49
  • Can you explain the logic a little better? I was expecting the output to be `{"#000000": 0, "#FFFFFF": 1, "#ff0000": 0.292, "#3498db": 0.7}` if you want to reverse order. As written, do you mean that each proeprty has to get the value of the next property? key 0 becomes value 1, key 1 becomes value 0, etc. But since the order of keys will vary alot in real world examples, there ahs to be some kind of logic that determines which values to switch. – Shilly Dec 20 '17 at 14:55
  • My interpretation of the logic requested: assume the keys are numbers. Return an object identical to the object you'd get if you ordered the keys numerically and reversed the associated values. – Anthony Mills Dec 20 '17 at 15:06

2 Answers2

0
function updateKVs(data) {
    // Grab the object properties and sort them
    const keys = Object.keys(data).sort();

    // grab the values
    const valuesRaw = Object.values(data);

    // convert them to integers and sort.
    // NOTE: you need to drop the initial `#` or it will return NaN
    const valuesSortedInts = valuesRaw.map((n) => parseInt(n.substr(1), 16)).sort()

    // convert back to hex. Include padding to make the value match inital.
    const valuesSortedHex = valuesSortedInts.map(
       (n) => n.toString(16).padStart(6, '0')
    );

    // Iterate through the keys, re-assigning them.
    keys.forEach((kv, i) => data[kv] = '#' + valuesSortedHex[i]);
    return data;
}

updateKVs({0: "#000000", 1: "#FFFFFF", 0.292: "#ff0000", 0.7: "#3498db"})
> {0: "#000000", 1: "#3498db", 0.292: "#ff0000", 0.7: "#ffffff"}
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0
function reverseValues(original) {
  // put into a combination array
  var combined = [];
  for (var key in original) {
    // note: keys are always strings! So use +key to convert to a number for sorting
    combined.push({key: +key, val: original[key]});
  }

  // sort it by the key value
  combined.sort((a, b) => a.key - b.key);

  // Create the result
  var result = {};
  var combinedMax = combined.length - 1;
  combined.forEach((kv, i) => result[kv.key] = combined[combinedMax - i].val);
  return result;
}

var testVal = {0: "#000000", 1: "#FFFFFF", 0.292: "#ff0000", 0.7: "#3498db"};
console.dir(reverseValues(testVal));
Anthony Mills
  • 8,676
  • 4
  • 32
  • 51