1

I have to return the array of the key-value pairs of object via a method. I'm using Object.entries(), but it changes the order of properties(in example you can see that numerical property is moved to the first place in returned array).

Object.toPairs = function (obj) {return Object.entries(obj);}

var aloha = {
  this: 'is',
  not: 'so',
  hard: 'really o_O ?!',
  a: 'xxx',
  1: '-----',
};
Object.toPairs(aloha)

result is

[ [ '1', '-----' ], [ 'this', 'is' ], [ 'not', 'so' ], [ 'hard', 'really o_O ?!' ], [ 'a', 'xxx' ] ]

How can it be properly solved?

Barmar
  • 596,455
  • 48
  • 393
  • 495
max 1337
  • 11
  • 2
  • Solve it by sorting if you can. Or be safe and use an array of objects. There are rules for the ordering, which I think now put numeric indexes first and sequential. Before that it was implementation defined, so there were no guarantees. –  Jan 10 '18 at 20:54
  • 4
    If you can, change `aloha` to a `Map`, it respects order when you insert. Otherwise you're going to have a hard time with hashes. – JohanP Jan 10 '18 at 20:59
  • How do you want the solution to look like? – a2441918 Jan 10 '18 at 21:05
  • solution for given example should look like this >> [ ['this', 'is'], ['not', 'so'], ['hard', 'really o_O ?!'], ['a', 'xxx'], [1, '-----'] ] – max 1337 Jan 10 '18 at 21:07

0 Answers0