2

I have a JSON object as follows

var allUsers = {
        "student_a":{
            id:1,
            full_name:"ABC",
            address:"xyz",
            image:"image url"
        },
        "student_b":{
            id:2,
            full_name:"DEF",
            address:"",
            image:"image url"
        },
         "student_c":{
            id:3,
            full_name:"",
            address:"",
            image:""
        }
    }

In the above JSON I need to figure out how many empty fields are there in each student.

I am using the following code

_submitInfo(allUsers) {
            var empty_fields = Object.entries(allUsers).map(([key, value]) => {
                return this._validateStudent(value)
            })
            alert(JSON.stringify(empty_fields))      
        }

_validateStudent(studentInfo) {   
            empty = 0;
            Object.entries(studentInfo).map(([key, value]) => {
                if (value == "") {
                    empty++
                }
            })
            return empty
        }

But the output I get is [0,0,0] the desired output is [0,1,3]. I think promises will solve the issue but I am unaware of how will I use them in this nested case.

3
  • your code returns [0, 1, 3] check again Commented Jan 11, 2019 at 7:14
  • No it dose not I just rechecked Commented Jan 11, 2019 at 7:18
  • check here link Demo Commented Jan 11, 2019 at 7:32

6 Answers 6

1

As you want to use promises with above problem I updated your code with promises, just take a look.

    var allUsers = {
        "student_a": {
            id: 1,
            full_name: "ABC",
            address: "xyz",
            image: "image url"
        },
        "student_b": {
            id: 2,
            full_name: "DEF",
            address: "",
            image: "image url"
        },
        "student_c": {
            id: 3,
            full_name: "",
            address: "",
            image: ""
        }
    }

check(allUsers);
    function check() {
        const promiseContainer = [];
        Object.entries(allUsers).map(([key, value]) => {
            promiseContainer.push(_validateStudent(value));
        });

        function _validateStudent(studentInfo) {
            let empty = 0;
            return new Promise((resolve, reject) => {
                Object.entries(studentInfo).map(([key, value]) => {
                    if (value == "") {
                        empty++
                    }
                })
                resolve(empty);
            });
        }

        Promise.all(promiseContainer).then((count) => { console.log(count) });
    }

Sign up to request clarification or add additional context in comments.

Comments

0

var allUsers = {
    "student_a":{
        id:1,
        full_name:"ABC",
        address:"xyz",
        image:"image url"
    },
    "student_b":{
        id:2,
        full_name:"DEF",
        address:"",
        image:"image url"
    },
     "student_c":{
        id:3,
        full_name:"",
        address:"",
        image:""
    }
}


var result = Object.keys(allUsers).map(student => Object.values(allUsers[student]).reduce((n, v) => {
    if(!v.toString().length) n += 1;
    return n
},0))
console.log(result)

Comments

0

This will be working properly.

 var data = {
        "student_a":{
            id:1,
            full_name:"ABC",
            address:"xyz",
            image:"image url"
        },
        "student_b":{
            id:2,
            full_name:"DEF",
            address:"",
            image:"image url"
        },
         "student_c":{
            id:3,
            full_name:"",
            address:"",
            image:""
        }


    }


     let output =[]
     for (let value of Object.values(data)) {

       var test = Object.values(value)
       var lucky = test.filter(function(number) {
      return number == "";
    });

       output.push(lucky.length)

    }


    console.log(output)

Comments

0

Perfect the below code is working

var result = Object.keys(allUsers).map(student => Object.values(allUsers[student]).reduce((n, v) => {
    if(!v.toString().length) n += 1;
    return n
},0))

but there is more issue, what if the JSON is as follows

var allUsers = {
        "student_a":{
            id:1,
            full_name:"ABC",
            address:"xyz",
            email:"[email protected]",
            number:"1234567890",
            image:"image url"
        },
        "student_b":{
            id:2,
            full_name:"DEF",
            address:"",
            email:"random value",
            number:"000",
            image:"image url"
        },
         "student_c":{
            id:3,
            full_name:"",
            address:"",
            email:"",
            number:"",
            image:""
        }
    }

and there is an email and phone number and if either invalid I consider it as blank. So in the above JSON the output should be [0,3,5] 3 empty fields for student_b as email and phone number is invalid. Where do it put this condition ?

Comments

0

const allUsers= {
    "student_a":{
        id:1,
        full_name:"ABC",
        address:"xyz",
        image:"image url"
    },
    "student_b":{
        id:2,
        full_name:"DEF",
        address:"",
        image:"image url"
    },
     "student_c":{
        id:3,
        full_name:"",
        address:"",
        image:""
    }
};

let arr=[];    
Object.entries(allUsers).map(([key,val]) => {
	 x= Object.values(val).reduce((count, cur) => {
	 	if(cur == ""){
	 	return ++count;
	 	}
	 	return count;
	 },0)
	arr.push(x);
});
console.log(arr);

Comments

0

For this format

var allUsers = {
    "student_a":{
        id:1,
        full_name:"ABC",
        address:"xyz",
        email:"[email protected]",
        number:"1234567890",
        image:"image url"
    },
    "student_b":{
        id:2,
        full_name:"DEF",
        address:"",
        email:"random value",
        number:"000",
        image:"image url"
    },
     "student_c":{
        id:3,
        full_name:"",
        address:"",
        email:"",
        number:"",
        image:""
    }
}

to get your output [0,3,5]

do this:

Object.keys(allUsers).map(obj => allUsers[obj]).map(item => { 
   item.email = /\S+@\S+\.\S+/.test(item.email) ? item.email : ""; // valid email or ""
   item.number = item.number ? item.number.match(/\d/g).length ===10 ? item.number : "" : item.number;  //valid number or ""
   return item; 
}).map(item => Object.values(item).filter(innerItem => innerItem === "").length);

this will print [0, 3, 5]

live demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.