I have array of object like this
let arr = [
{
"james-0": {
"value": "",
"error": false
},
"john-0": {
"value": "",
"error": false
},
"jordan-0": {
"value": "",
"error": false
}
}
]
I want to alter error to true if the value is not false / empty string.
I tried this but it deosn't seem to work, I can't hardcode james-${i}
arr = arr.map((o,i) => {
let prop = o[`james-${i}`]
if(!prop.value) prop.error = true
console.log(prop)
return {
...o,
[`james-${i}`]: prop
}
})
You can try this approach
let arr = [ { "james-0": { "value": "", "error": false }, "john-1": { "value": "", "error": false }, "jordan-2": { "value": "val", "error": false } } ]; arr = arr.map(value => { Object.keys(value).forEach(key => { if (!value[key].value) { value[key].error = true; } }) return value; }) console.log(arr);
.
simply that ?
let arr = [ { "james-0": { "value": "", "error": false } , "john-0": { "value": "abc", "error": false } , "jordan-0": { "value": "", "error": false } } ] Object.keys(arr[0]).forEach( e=> arr[0][e].error = ( arr[0][e].value == '')) console.log( arr )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.