Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在循环中的对象数组中推送值?_Javascript_Arrays_Reactjs_Object_State - Fatal编程技术网

Javascript 如何在循环中的对象数组中推送值?

Javascript 如何在循环中的对象数组中推送值?,javascript,arrays,reactjs,object,state,Javascript,Arrays,Reactjs,Object,State,如何在循环中以react状态推送objectlist中的数组 我的默认状态: this.state = { users: [ { id:1, name: 'John' }, { id:2, name: 'Nick'

如何在循环中以react状态推送objectlist中的数组

我的默认状态:

this.state = {
            users: [
                {
                    id:1,
                    name: 'John'
                },
                {
                    id:2,
                    name: 'Nick'
                }
            ]
        };
我试过这样的东西,但不起作用

changeHandler (currentUser, newValue) {
        const users = this.state.users;
        for (var i in users) {
            if (users[i].id == currentUser) {
                this.setState({
                    users: [...this.state.users[i], {newValue: newValue}]
                })
            }
            break;
        }
    }
因此,我希望看到:

default state - {id:1,name: 'John'},{id:2, name: 'Nick'} 
use changeHandler(2,1) - {id:1,name: 'John'},{id:2, name: 'Nick', newValue: [1]}
use changeHandler(2,2) - {id:1,name: 'John'},{id:2, name: 'Nick', newValue: [1,2]}
use changeHandler(2,3) - {id:1,name: 'John'},{id:2, name: 'Nick', newValue: [1,2,3]}
您可以使用地图:

this.state.users.map(
  (user)=>user.id===currentUser
    //if user id is current user then copy user to new object {...user}
    //set newValue to user.newValue or empty array newValue:(user.newValue||[])
    //add the newValue to user.newValue: .concat(newValue)
    ? {...user,newValue:(user.newValue||[]).concat(newValue)
    //user is is not currentUser, just return the user as is
    : user
)

您可以使用扩展语法映射和更新状态,如

changeHandler (currentUser, newValue) {
    const users = this.state.users;
    this.setState(prevState => ({
       users: prevState.users.map((user => {
           if (user.id == currentUser) {
              return {
                  ...user,
                  newValue: [...(user.newValue || []), newValue]
              }
           }
           return user
       }))
    }))
}
简短的一点:

changeHandler currentUserId,newValue{ this.setState{users}=>{ 用户:users.mapuser=>{ 使用者 ... user.id==当前用户id ?{newValue:[…user.newValue | |[],newValue]} : {} , } }; } 长的一个:

changeHandler currentUserId,newValue{ this.setState{users}=>{ const otherUsers=users.filter{id}=>id!==currentUserId; const currentUser=users.find{id}=>id==currentUserId; 返回{ 用户:[ …其他用户, { …当前用户, 新价值:[ …currentUser.newValue | |[], 新价值 ], }, ], }; }; } 长版本的短版本:

changeHandler currentUserId,newValue{ this.setState{users}=>{ 用户:[ …users.filter{id}=>id!==currentUserId, { …users.find{id}=>id==currentUserId, newValue:[…currentUser.newValue | |[],newValue], }, ], }; }
在React中,状态应保持不变,您必须使用setState方法来更新它。@mab是的,完整版本为:this.setState{…this.state,用户:this.state.users.map--