0

I want the following call to my function

MakeMargin(unit1)

To ouput the following variable ( not a string ! ):

theme.spacing.unit1

I tried the following :

function MakeMargin(props) {
  const output = theme.spacing + props

  return (
    output
  );
}
Romainpetit
  • 897
  • 2
  • 11
  • 29
  • Vote to close because this question is unclear. Please share i.e: error output – Ele Jan 31 '18 at 13:03
  • 1
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – str Jan 31 '18 at 13:03
  • Also, take a look [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Meta_programming) – Federico klez Culloca Jan 31 '18 at 13:03
  • Just to clarify, are you trying to output the content of that variable or the variable itself? I mean, are we talking about metaprogramming? – Federico klez Culloca Jan 31 '18 at 13:05
  • Probably the OP wants to pass a String, i.e: `MakeMargin("unit1")` and within `MakeMargin` returns `"theme.spacing" + props`. It's unclear!" – Ele Jan 31 '18 at 13:06
  • @ele judging from the comment to faly's answer I'm afraid we are both wrong. – Federico klez Culloca Jan 31 '18 at 13:08
  • @FedericoklezCulloca no, the question is asking for concatenation and not for object accessing. It's unclear. – Ele Jan 31 '18 at 13:09
  • I wasn't sure that concatenation was the right term here. Thanks everybody – Romainpetit Jan 31 '18 at 13:09

1 Answers1

2

It should be:

function MakeMargin(props) {
    return theme.spacing[props]
}
Faly
  • 13,291
  • 2
  • 19
  • 37