-2

I am trying to create an object that looks something like this:

{
    '2017': 2017,
    '2016': 2016,
    '2015': 2015,
    '2014': 2014
}

But for whatever reason, it keeps outputting me ascending order instead:

const EXAMPLE = () => {
    const exampleObj = {}

    for (let year = 2017; year >= 2014; year--) {
        exampleObj[year] = year
    }   


    return exampleObj
}

let example = EXAMPLE()
console.log(example)

Is it possible to achieve of what I desire in Javacript? I would choose Map but in my application, I need an object to render a certain component

Alejandro
  • 2,136
  • 4
  • 32
  • 61

1 Answers1

0

No, this is not possible to achieve when you use an Object's keys. You should use a Map instead.

4.3.3 Object

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

Mateusz Kleinert
  • 1,218
  • 11
  • 19