-3

I know how to change the value of a object but is it possible to change the name of a key or change the index position of a key?

const object1 = {
  "a": 'helloa',
  "b": 'hellob',
  "c": 'helloc',
  "d": 'hellod'
};

console.log(object1.b);
// hellob

What I want:
// "a": 'helloa'
// "c": 'helloc'
// "b": 'hellob'
// "d": 'hellod'

Solution:

Help from @line-o

The solution is to put all values ​​in an object and then change the order of the array. A sorting algorithm then changes the values ​​of the object with the values ​​of the array.

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

const order = ["a", "b", "c", "x", "d", "e"];

// add new item in list
object1["x"] = "6" ;

// Change order of items
var a = order.indexOf("a");
var e = order.indexOf("e");

if (~a) {
    order[a] = "e";
}

if (~e) {
    order[e] = "a";
}

const sorted = Object
  .keys(object1)
  .sort(function (a, b) { 
    /* my sorting algo */
    const indexA = order.indexOf(a)
    const indexB = order.indexOf(b)
    
    return indexA - indexB;

  });

object1 = order.map(key => object1[key]);


console.log(object1);

Tim
  • 19
  • 4
  • Both input and 'what i want' are the same? – 0stone0 Apr 15 '21 at 13:02
  • 3
    Keys don't really have an "index position" that is useful to software design. You cannot change the name of an object property, but you can create a new property with a new name, assign the value of the old property, and then delete the old property. – Pointy Apr 15 '21 at 13:03
  • 2
    First of all, you should probably go check https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order - because an order is not always guaranteed to begin with. – CBroe Apr 15 '21 at 13:04
  • 1
    @CBroe - Yes it is. It's just not useful. :-) – T.J. Crowder Apr 15 '21 at 13:05
  • 1
    You could use `Object.entries(object1)`, that will give you an array which can be reordered? – evolutionxbox Apr 15 '21 at 13:05
  • 1
    Probably best to explain the higher level problem you are trying to solve by doing this. Other solutions can then be offered – charlietfl Apr 15 '21 at 13:06
  • @T.J.Crowder I was referring to what’s said at the end of this answer, https://stackoverflow.com/a/38218582/1427878, that not all methods that might be used to access the keys, will guarantee that order - which directly touches on the “usefulness” issue as well, IMHO. – CBroe Apr 15 '21 at 13:09
  • 2
    @CBroe - That answer is out of date, [even `for-in`](https://github.com/tc39/proposal-for-in-order) and various methods that used the same underlying operation have a defined order now (in non-edge cases). (And we agree on **still** not advocating using it. ) – T.J. Crowder Apr 15 '21 at 13:14
  • (I've fixed it now.) – T.J. Crowder Apr 15 '21 at 13:27

1 Answers1

0

There is two parts to this story:

Given

const order = ["melon", "pumpkin", "beard", "lizard"];

const object1 = { 
  "?": "!", 
  "pumpkin": "spice", 
  "beard": "shaved",
  "lizard": "green",
  "melon": "seed"
};

Is there a specific order and unknown properties should be omitted?

console.log(order.map(key => object1[key]))

Do you want to order all properties using a certain sorting function:

const sorted = Object
  .keys(object1)
  .sort(function (a, b) { 
    /* or any sorting algorithm (alphabetical, numerical, ...) */
    const indexA = order.indexOf(a)
    const indexB = order.indexOf(b)
    
    return indexA - indexB;

  });
console.log(sorted.map(key => object1[key]))

jsbin

line-o
  • 1,661
  • 3
  • 15
  • 29