Jennifer Jennifer - 2 years ago 92
Javascript Question

validation using forEach to get property key

var field_arr = [{name:name},{email:email},{tel:tel}];

How to get the value of name, email or tel (property key)? I want to use loop to prompt user what is missing. Like if user missed tel, there will be an alert saying
tel is missing

Answer Source

I don`t think you need it to be an array. Just loop over all the properties of your JSON object.

 var obj = {
   name: "name",
   email: "",
   tel: "tel"
 $.each(obj, function(key, value) {
   if (value == "") {
     console.log(key + ": " + value);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download