1

I have a function called myObject that returns an object by taking two parameters, the name of the key and the value. The problem is that the parameters we give as key and value only consider and display the value but not the key. You can see the output in the console. please guide me

The desired output in the console : {"Number":12345}

const myObject = (key, value) => {
    return { key: value }
};
console.log(myObject("Number", 12345));
jarmod
  • 71,565
  • 16
  • 115
  • 122
goodboy
  • 31
  • 4
  • 2
    Put `key` in square brackets like `{ [key]: value }` – mousetail Jul 27 '22 at 14:51
  • You'd probably want to read up on [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names) – Trash Can Jul 27 '22 at 14:52
  • Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#new_notations_in_ecmascript_2015 - the examples under `// Computed property names (ES2015)` – James Jul 27 '22 at 14:53

1 Answers1

-1

You are creating an object with a key called key. Instead you want your function to look like this:

const myObject = (key, value) => {
  return { 
    [key]: value
  }
};

console.log(myObject("Number", 12345))
Riley Conrardy
  • 392
  • 1
  • 9
  • 17