Javascript 如何在映射函数中使用分解赋值?

Javascript 如何在映射函数中使用分解赋值?,javascript,reactjs,Javascript,Reactjs,我想使用解构分配内部映射函数。。。我怎样才能正确地为工作做这些?我需要一份道具的副本,不要参考。。。我只需要更改状态,而不是道具。您可以尝试下一步: constructor(props){ super(props) let Path = window.location.href.split('/') let name = Path[Path.length-1] name = name.replace(/%20/g, ' ') const auxKeys =

我想使用解构分配内部映射函数。。。我怎样才能正确地为工作做这些?我需要一份道具的副本,不要参考。。。我只需要更改状态,而不是道具。您可以尝试下一步:

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'
}

const{val1}=this.props.test.aux[value],
这不是一个扩展运算符,更像是对象解构。为什么在这里使用
map
?您对结果数组不做任何操作。为什么要在三元表达式中指定
aux
。那个无效的语法一点也不清楚。输入是什么(
this.props.test.aux
),预期输出是什么?解构、扩展和
map
方法与此有什么关系?@Bergi我知道你说得对:扩展语法不是运算符,但从我的母语开始,它被传为
传播算子
:El operador de propagación传播算子permite que una expresión ses esperan múltiples argumentos(函数)o múltiples elementos(数组文字)。也许同样的翻译适用于其他语言,这会产生混乱。我需要复制这个.props.test.aux[value]。“some”,因为我会将这个值放在state={}中,当我更改状态时,我不想更改props。。。我改变了提问,看,如果你不想使用引用,你可以通过object.assign,const newAux=object.assign({},this.props.test.aux)创建新的道具对象,它也不起作用:/。。我只需要一个副本,比如
const{val1}=this.props.test.aux[value]
我使用:
valx:[…this.props.test.aux[value]
。感谢您的帮助@MaksymKoldun
  auxKeys.map((value, key) => this.props.test.aux[value].name === name ?  
        aux = {val0: value, ...this.props.test.aux[value]}:
        null