2
constructor(props){
    super(props)
    let Path = window.location.href.split('/')
    let name = Path[Path.length-1]
    name = name.replace(/%20/g, ' ')
    const auxKeys = Object.keys(this.props.test.aux) 
    let aux = {}

    auxKeys.map((value, key) => this.props.test.aux[value].name === name ?  
        aux = {
            val0: value,
            const {val1} = this.props.test.aux[value], //i need to do this, but doesn't work
            val2: this.props.test.aux[value].val2, //when i do this, i pass the reference of props, and when i change the state, i change the props... but if user cancel this operation, the props was changed yet
            val3: this.props.test.aux[value].val3,
            val4: this.props.test.aux[value].val4,
            val5: this.props.test.aux[value].val5
        }:
        null  
    )

    const {val0, val1, val2, val3, val4, val5} = aux 

    this.state = {
        aux: {
            val0,
            val1,
            val2,
            val3,
            val4,
            val5,

        }
    }
} 

handleChange = field => event => {
const aux = {
   ...this.state.aux,
   [field]: event.target.value
}
this.setState({aux})  //when i do this, i don't want to change the props, i want just change this.state.aux.'field'
}

i want to use destructuring assignment inter map function... how can i do this for work corretly? i need to take a copy of props and don't reference... i need to change just state, and not props

  • 1
    `const {val1} = this.props.test.aux[value],` this is not a spread operator, more like object deconstructing. – Adrian Feb 26 '19 at 21:43
  • [Spread syntax is not an operator](https://stackoverflow.com/q/35019557/1048572) – Bergi Feb 26 '19 at 21:47
  • Why are you using `map` here? You do nothing with the result array. And why do you assign to `aux` inside a ternary expression? – Bergi Feb 26 '19 at 21:48
  • Sorry, I don't get what you want. That invalid syntax is not clear at all. What is the input (`this.props.test.aux`), and what is the expected output? What do destructuring, spread, and the `map` method have to do with it? – Bergi Feb 26 '19 at 21:49
  • @Bergi I know you are right about: _spread syntax is not an operator_, but from [MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/Spread_operator) on my native language it is traduced as `spread operator`: _El operador de propagación **spread operator** permite que una expresión sea expandida en situaciones donde se esperan múltiples argumentos (llamadas a funciones) o múltiples elementos (arrays literales)._ Maybe the same translation apply to other languages and that generates confusion. – Shidersz Feb 26 '19 at 21:57
  • @Shidersz It's not a translation issue, it's just inaccuracy. MDN is a wiki, and its contributors are only humans. The term "spread operator" was (and unfortunately still is) very popular, so this mistake is repeated over and over. – Bergi Feb 26 '19 at 22:00

1 Answers1

0

you can try next:

  auxKeys.map((value, key) => this.props.test.aux[value].name === name ?  
        aux = {val0: value, ...this.props.test.aux[value]}:
        null
Max
  • 781
  • 7
  • 19
  • i need take a copy of this.props.test.aux[value].'some', because i will put this value in state= {}, and when i change the state, i don't want to change the props... i changed the ask, look that plz – maria mariadobairro Feb 26 '19 at 22:49
  • if you do not wanna use reference, you could make new object of props via Object.assign , const newAux = Object.assign({}, this.props.test.aux) – Max Feb 26 '19 at 23:04
  • it doesn't work too :/ .. i need just a copy, like `const {val1} = this.props.test.aux[value]` – maria mariadobairro Feb 27 '19 at 00:31
  • i do this with: `valx: [...this.props.test.aux[value].valx]`. tnkz for your help @MaksymKoldun – maria mariadobairro Feb 27 '19 at 01:06