简体   繁体   English

如何在地图功能中使用解构分配?

[英]How can i use destructuring assignment into map function?

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 我需要复制道具而不参考...我需要更改状态,而不是道具

you can try next: 您可以尝试下一个:

  auxKeys.map((value, key) => this.props.test.aux[value].name === name ?  
        aux = {val0: value, ...this.props.test.aux[value]}:
        null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对象分解任务的左侧使用它? - How can I use this in the left side of the object destructuring assignment? 如何减少我的 function? 我收到以下错误:Expect to use destructuring assignment - {balance} - How to reduce my function? I get the following error : Expect to use destructuring assignment - {balance} 不能使用 this.state 因为我使用 eslint,“必须使用解构状态赋值”/解构赋值反应 - can't using this.state because i use eslint, “Must use destructuring state assignment” /destructuring-assignment react 如何为功能性React组件props实施解构分配? - How can I implement destructuring assignment for functional react components props? 如何修复 eslint: react/destructuring-assignment 错误? - How can I fix eslint: react/destructuring-assignment error? 如何结合解构赋值和可选链接? - How can I combine destructuring assignment and optional chaining? 如何在 js 中清理这个解构赋值? - How can I clean up this destructuring assignment in js? 有没有办法可以重构结构分配的一部分? - Is there a way I can restructure part of a destructuring assignment? 如何在对象内部使用解构分配 - How to use a destructuring assignment inside an object 用函数参数破坏分配 - Destructuring assignment with function arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM