0

I have an object like this :

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 a loop to prompt user what is missing. For example, if user missed tel, there will be an alert saying tel is missing.

2
  • Could you not use a single object? Then it would be simple to loop through Object.keys once. Commented Apr 24, 2016 at 11:34
  • Assuming this is for a form, I'd definitely use the required attribute instead. Commented Apr 24, 2016 at 11:48

4 Answers 4

1

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);
   }

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

3 Comments

in pure js without $.each I used for(key in obj) instead.
Don't use for(key in obj), especially without .hasOwnProperty guard. Use Object.keys(obj) instead.
If obj doesn't have some key at all likevar obj = {name: "name",tel: "tel" }; it won't log. There is no validation against anything and just checking if obj has any empty values. Check my solution.
0

Assuming you are storing those values from some fields with jQuery or something else. you can do something like :

var field_arr = [{name:"name"},{email:""},{tel:"lol"}];
jQuery.each(field_arr,function(obj){
        for (var i in obj) {
            if (obj[i].trim() === "") {
                 console.log(i + " is missing");
            }
        } 
 });

Comments

0

Assuming filed_arr is user input and you can't change the form of it and you want to compare it with required fields

var field_arr = [{name:'abc'},{email:'xyz'}];

var required = ['name', 'email' , 'tel'];

var inputKeys = field_arr.reduce(function(prev,next){
  return prev.concat(Object.keys(next));
},[]);

document.write('<pre>inputKeys => ' +inputKeys+'</pre>');

var missingKeys = required.filter(function(key){
  return inputKeys.indexOf(key) == -1;
});

document.write('<pre>missingKeys => ' +missingKeys+'</pre');

alert('You are missing '+missingKeys);

Comments

0

You want to :

  • turn your field_arr object into a key=>value type object
  • then loop on your required list to check that each item is a key in that object :

Code :

var arr = {};
for(var i=0; i<field_arr.length; i++){
   var key = Object.keys(field_arr[i])[0];
   arr[key] = field_arr[i][key];
}

// At this point you have a key => value type array in "arr"

var required_list = ["name", "email", "tel"];
for(var i=0; i<required_list.length; i++){
  var item = required_list[i];
  if(! item in arr) {
     alert(item+" is missing");
  }
}

If you want to use forEach instead of a for loop :

var arr = {};
field_arr.forEach(function(obj){
      var key = Object.keys(obj)[0];
      arr[key] = obj[key];
   });
}

// At this point you have a key => value type array in "arr"

var required_list = ["name", "email", "tel"];
required_list.forEach(function(item){
   if(! item in arr)
      alert(item+" is missing");
});

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.