[英]Update specific properties in specific object
I just get little stuck on quite simple task. 我只是停留在相当简单的任务上而已。 Apologise in advance if this question is already answered.
如果已经回答了这个问题,请提前道歉。 I just can not find it.
我就是找不到。 I got objects in state
我有物体处于状态
state = {
controls: {
email: {
elementConfig: {
type: 'email'
},
placeholder: 'Email Address',
value: '',
validation: {
required: true,
isEmail: true
},
valid: false,
touched: false,
errorMsg: ""
},
password: {
elementConfig: {
type: 'password'
},
placeholder: 'Password',
value: '',
validation: {
required: true,
minLength: 6
},
valid: false,
touched: false,
errorMsg: ""
}
},
};
and I need to update "touched" to true and "errorMsg" to a text only if "valid" in the object equal to false 并且仅当对象中的“有效”等于“假”时,才需要将“触摸”更新为“真”,将“ errorMsg”更新为文本
I managed to filter all object keys with "valid" value equal to false 我设法过滤所有具有“有效”值等于false的对象键
const invalidInputs = Object.keys(this.state.controls).filter(key => {
return this.state.controls[key].valid === false
})
Now I need to loop through each "invalidInputs" and update touched to true and "errorMsg" to a specific text. 现在,我需要遍历每个“ invalidInputs”,并将touched更新为true,将“ errorMsg”更新为特定文本。 I have tried
我努力了
for (let key in invalidInputs){
const updatedControl = {
...this.state.controls,
[invalidInputs[key]]: {
...this.state.controls[invalidInputs[key]],
touched: true,
errorMsg: errorMsg(this.state.controls[invalidInputs[key]].validation, this.state.controls[invalidInputs[key]].value)
}
};
this.setState({controls:updatedControl})
console.log(updatedControl)
}
but that doesn't work obviously. 但这显然不起作用。 Can anyone give me some nice solution or direct me to an answer?
谁能给我一些不错的解决方案或指导我解决问题?
Thanks in advance 提前致谢
Use for..in
to loop through the object and check the value of valid
使用
for..in
对象并检查valid
let state = { controls: { email: { elementConfig: { type: 'email' }, placeholder: 'Email Address', value: '', validation: { required: true, isEmail: true }, valid: false, touched: false, errorMsg: "" }, password: { elementConfig: { type: 'password' }, placeholder: 'Password', value: '', validation: { required: true, minLength: 6 }, valid: false, touched: false, errorMsg: "" } }, }; for (let keys in state.controls) { if (!state.controls[keys].valid) { state.controls[keys].errorMsg = 'New Msg'; state.controls[keys].touched = true; } } console.log(state)
You can use map()
and change the object according to needs instead of using filter()
. 您可以使用
map()
并根据需要更改对象,而不是使用filter()
。
let state = { controls: { email: { elementConfig: { type: 'email' }, placeholder: 'Email Address', value: '', validation: { required: true, isEmail: true }, valid: false, touched: false, errorMsg: "" }, password: { elementConfig: { type: 'password' }, placeholder: 'Password', value: '', validation: { required: true, minLength: 6 }, valid: false, touched: false, errorMsg: "" } }, }; let result = Object.keys(state.controls) .map((con) => (!state.controls[con].valid ? ({...state.controls[con],errorMsg:"New msg",touched:true}) : ({...state.controls[con],touched:true}))) console.log(result);
I just wanted to post a solution which works for me which is the same as the first solution but without mutating state 我只想发布一个对我有用的解决方案,该解决方案与第一个解决方案相同,但不更改状态
const updatedControls = { ...this.state.controls }
for (let keys in updatedControls) {
if (updatedControls[keys].valid === false) {
const control = updatedControls[keys]
control.errorMsg = "new msg"
control.touched = true
updatedControls[keys] = control
}
}
this.setState({ controls: updatedControls })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.