Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check all the values of an object then if the values is empty it will return what is the key of that empty value?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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] === ''))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading