I have an object store in useState, i just want to find out which of this keys have a value of empty and return the key of that emply value
const [projectInfo, setProjectInfo] = useState({
projectName: '',
projectDescription: '',
projectCreator: '',
})
const [error, setError] = useState({})
const handleCheckValidation = () => {
if(!projectInfo.projectName){
setError((prevState) => ({
...prevState,
projectNameError: true,
}))
Thanks!
CodePudding user response:
you can filter the empty value, it will give you array item of empty value,
const isEmptyValue = Object.keys(projectInfo).filter((item) => projectInfo[item] === '')
output: ["projectName", "projectDescription", "projectCreator"]
or if the object is dynamic, and you are getting it through props or api
const [projectInfo, setProjectInfo] = useState(Object.keys(yourObject).filter((item) => yourObject[item] === ''))
CodePudding user response:
const object = {
projectName: '',
projectDescription: '',
projectCreator: '',
}
const nullKeys = []
Object.keys(object).forEach(key => {
if(!object[key]){
nullKeys.push(key)
}
})
console.log(nullKeys)
