I
I
ivankirshin2018-05-30 00:29:40
JavaScript
ivankirshin, 2018-05-30 00:29:40

Why doesn't destructuring work?

Faced a problem. I need to swap objects that are stored in an array in vuex.

replacePuzzles({commit, getters},{first,second}){
        let puzzles = getters.getPuzzles 
        console.log(puzzles[first],puzzles[second])
        [puzzles[first],puzzles[second]] = [puzzles[second],puzzles[first]]

This error is displayed Cannot set property '#< Object >' of undefined.
Moreover, objects are normally output to the console. It is strange that if we remove the output to the console, then another error occurs: TypeError: Cannot read property '19' of undefined. (19 is the value of the variable).
I don't know what's the matter.
The project is here. Problem file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AngReload, 2018-05-30
@ivankirshin

You don't have semicolons, and the language itself doesn't put one between the closing parenthesis and the opening square bracket. Your code is similar to this:
It should be like this:

replacePuzzles({commit, getters},{first,second}){
        let puzzles = getters.getPuzzles 
        console.log(puzzles[first],puzzles[second])
        ;[puzzles[first],puzzles[second]] = [puzzles[second],puzzles[first]]

B
Bhudh, 2018-05-30
@Bhudh

The expression puzzles[first]returns a result, so it cannot be used in assignment expressions. That is why, by the way, objects are displayed in the console .
And then you try to assign each other to these objects (and not properties!)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question