1

I have an object with some properties like

weather.name = 'Coudy'
weather.country= 'USA'

Currently I am using Object destructuring in ES6 but as you can see it is quite verbose. I would like to know if it is possible to rewrite in a more concise way this code.

Notes: I am using babel with

  "presets": ["es2015", "react", "stage-2"]

const Weather = ({ weather }) => {
  let {
    name,
    country,
    temperature,
    temperatureMin,
    temperatureMax,
    weatherMain,
    weatherDescription,
    weatherIcon,
    updatedTime,
    windDegree,
    windSpeed,
    visibility
 } = weather
  return (<div>
    { name }, { country }
    { temperature }
    { temperatureMin }
    { temperatureMax }
    { weatherMain }
    { weatherDescription }
    { weatherIcon }
    { updatedTime }
    { windDegree }
    { windSpeed }
    { visibility }
  </div>
  )
}

export default Weather
2
  • you want to print all the value of weather object or some specific one ? Commented Apr 30, 2017 at 13:34
  • what about ({ weather }) => (<div> { Object.values(weather).join("\n") } </div>) Commented Apr 30, 2017 at 14:10

2 Answers 2

2

Yes, it's possible with parameter destructuring. weather temporary variable can be efficiently skipped (and Weather too - it isn't a constructor anyway):

export default ({ weather: {
    name,
    country,
    temperature,
    temperatureMin,
    temperatureMax,
    weatherMain,
    weatherDescription,
    weatherIcon,
    updatedTime,
    windDegree,
    windSpeed,
    visibility
 } }) => (<div>
    { name }, { country }
    { temperature }
    { temperatureMin }
    { temperatureMax }
    { weatherMain }
    { weatherDescription }
    { weatherIcon }
    { updatedTime }
    { windDegree }
    { windSpeed }
    { visibility }
  </div>)

All property names still should be written twice during destructuring, and this is a good thing here. This allows to pick only known properties from the object. If some of the used props weren't destructured or were misspelled in returned value, an error will be thrown. And if some of destructured props weren't used by accident, this can be indicated by IDE or a linter.

Sign up to request clarification or add additional context in comments.

2 Comments

i think all the values will be undefined because they exist in props.weather not directly inside props ? he is destructuring twice.
Yes, it appears that you're right. Thanks for pointing at that.
0

This is unsafe because it uses eval function, but you can do something like this:

const Weather = ({ weather }) => {
  eval(
    "var " +
    Object
      .keys(weather)
      .map(key => key + "="+ JSON.stringify(weather[key]))
      .join(', '));

  return (<div>
          {name}, {location}, 
          {anyAttribute.join(', ')}
  </div>
  )
};

ReactDOM.render(
  <Weather weather={{
    name: "lalala",
    location: "dadada",
    anyAttribute: [1,2,3]}} />,
  document.getElementById("root")
);

1 Comment

It should be noticed that eval makes possible the usage of with, which makes the thing simpler. But yes, eval is evil, and with isn't very kind, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.