I have the following code in react. Trying to iterate over specific keys using array of keys in object which has keys with null values produces error data[title] is null
like following :
let data = this.state.data
["description", "title"].forEach((key) =>
{
if(data[key] == "" || data[key] == null)
{
noErrors = false
errorsUpdate.push(`${key} field cannot be blank`)
}
})
However, using any of the following will not produce errors:
let data = this.state.data
let a = ["description", "title"]
for(let i = 0; i < a.length; i++)
{
if(data[a[i]] == "" || data[a[i]] == null)
{
noErrors = false
errorsUpdate.push(`${a[i]} field cannot be blank`)
}
}
let data = this.state.data
let a = ["description", "title"]
for(let i of a)
{
if(data[i] == "" || data[i] == null)
{
noErrors = false
errorsUpdate.push(`${[i]} field cannot be blank`)
}
}
I don't have deep knowledge in JS. My question why browser produce an error when using forEach
?