I wanted to use a destructuring solution to reorder my array within my React container which it should be pretty straight forward.
Given an array
a1 = ['hello', 'hi', 'hola']
componentDidMount() {
const { a1 } = this.props
this.a2 = []
[a2[2], a2[0], a2[1]] = a1 --> this line give an error!!
console.log('new ordered array', a2) // ['hola', 'hello', 'hi'] --> this print properly my new array
}
undefined
Uncaught (in promise) TypeError: Cannot set property '#<Object>' of undefined
console.log
This line gives you an error because this is one of those rare cases when automatic semicolon insertion lets you down. Never start new line with (
or []
, otherwise interpreter considers it as continuation of the previous line and treats [
or (
as property access operation or function invocation.
This will work:
this.a2 = [];
[a2[2], a2[0], a2[1]] = a1
Or this:
this.a2 = []
;[a2[2], a2[0], a2[1]] = a1