4

I've a problem: i would like to move a key/value of an object by one position, I'm using Javascript, i could also use Lodash, but i cannot transform the object into an array.

My object

"input": {
            "10_": "ab",
            "20_": "cd",
            "30_": "ef"
          }

Result

"input": {
            "20_": "cd",
            "10_": "ab",
            "30_": "ef"
          }

Thank you.

rikg93
  • 1,101
  • 1
  • 11
  • 21
  • 1
    in this case, i suggest to use an array with the ordered key for access. i would not rely on the order of properties. [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Nina Scholz Sep 06 '19 at 07:16
  • If you need an ordered object, use an array. Though ES2015 specifies some ordering for object's properties, that can't be used like this. – Teemu Sep 06 '19 at 07:16
  • you can also use Set which preserves order. http://xahlee.info/js/javascript_set.html – Harpreet Singh Sep 06 '19 at 07:17
  • whats the pattern to change the values? – Ajay Kumar Oad Sep 06 '19 at 07:17
  • JavaScript objects don't have ordering. Use an array instead as mentioned in [Move object element position in javascript](https://stackoverflow.com/questions/25261002/move-object-element-position-in-javascript) – Sudhir Ojha Sep 06 '19 at 07:17
  • possible duplicate of [object-property-order](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Krzysztof Krzeszewski Sep 06 '19 at 07:18
  • Is there any logic to the desired order? The keys aren't in numeric order, and the values aren't in alphabetic order. – Barmar Sep 06 '19 at 07:19
  • Yes, because each key/value is a row in a table, i want move up or down a row, then i want move key/value in the object. My problem is that I must send to be this object, not an array, maybe I'll use a set. – rikg93 Sep 06 '19 at 07:22
  • Storing an ordered table as object like that does not make any sense. –  Sep 06 '19 at 07:24
  • Changing the order is not useful, if you're sending the object to a server as a JSON string. JSON.stringify isn't guaranteed to preserve the order of object's properties. And the final result depends on how the order is created at the server when parsing JSON to the used native language, – Teemu Sep 06 '19 at 07:25
  • Chris G maybe for you, don't judge other job if you don't know the situation. – rikg93 Sep 06 '19 at 07:26
  • 1
    Making sense is not task dependent, it's language dependent. An object is like a sack where you drop the properties in. And when you read the properties, they are lifted from the sack one by one more or less randomly until the sack is empty. ES2015 Set and Map are only making a hole to the bottom of the sack, and reading the properties in the order you've originally dropped them in. But, in the meanwhile, you've shaken the sack. – Teemu Sep 06 '19 at 07:41

3 Answers3

1

In javascript object property order is equivalent to insertion order for string keys and ascending order for number keys, that being the case you can simply insert them in an order you want to obtain a desired result

const input = {
  "10_": "ab",
  "20_": "cd",
  "30_": "ef"
}

console.log({"20_": input["20_"],"10_": input["10_"],"30_": input["30_"]})

or slightly more general

const input = {
  "10_": "ab",
  "20_": "cd",
  "30_": "ef",
  "40_": "ef",
  "50_": "ef"
}

const [firstKey, secondKey] = Object.keys(input);
const {[firstKey]: firstKeyValue, [secondKey]: secondKeyValue, ...rest} = input

console.log({[secondKey]: secondKeyValue, [firstKey]: firstKeyValue, ...rest })
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
0

Positioning of object values have no impact in the Object and how it's properties and methods called. The object properties and methods are called and set only by their keys.

Ranjith
  • 137
  • 1
  • 1
  • 7
  • 2
    The order may be important depending on your design. If you loop through the object properties, the order might be important. – Yassir Ennazk Jul 30 '21 at 19:38
0

An object is unlike an array, the order does not matter.

When retrieving data from an array, you might need to iterate through the entire array. But for an object that has key-value pairs, you just need to access the value using the key.

For example, to access the value of _10 (below), you just have to write fruits.a or fruits['a']

const fruits = {
            'a': 'apple' ,
            'b': 'banana'
          };

const fruits2 = {
            'b': 'banana',
            'a': 'apple' 
          };

JavaScript treats both objects the same way and you get the same output when you do fruits.a and fruits2.a

XingZhi Lim
  • 131
  • 2
  • 6