1

how can i check if any javaScript object's property exists and if it exists then it has a valid value? actually,i am a beginner and trying to solve this-

Check if the second argument is truthy on all objects of the first argument(which is an array of objects).i.e.

  • check if the second argument exists in all the objects in first argument(an array) as a property.
    if it exists, it should not be-
  • invalid, as age can't be 0
  • null
  • undefined
  • empty string('')
  • NaN

till now i have this-

function truthCheck(collection, pre) {

  for(var i=0;i<collection.length;i++){
    if(!(pre in collection[i])||collection[i]     

      [pre]===undefined||isNaN(collection[i]      

      [pre])||collection[i][pre]===""||           

      collection[i][pre]===null||collection[i]    

      [pre]===0)
       {
          return false;
       } 
   }
 return true;
}

i know this is not the best wayto solve .Is there a better way to do this?i don't like that long if statement in my code.i have seen other SO links-link1,link2 but none of them seemed to solve my query. any kind of help is highly appreciated. P.S. this code is not working for some true cases even.

Community
  • 1
  • 1

3 Answers3

1
o = new Object();
o.prop = 'exist';

if(o.hasOwnProperty('prop')){
   if(o['prop']){
     alert('good value')
   }
}

https://stackoverflow.com/a/6003920/1074179

Community
  • 1
  • 1
Alexandr
  • 5,460
  • 4
  • 40
  • 70
  • no,this is not working .this is just working for checking the if key exists or not.P.S i want concise code. :) – Srishti Sharma Jun 22 '16 at 12:42
  • 1
    hasOwnProperty('prop') checks if the property exist and then o['prop'] checks for empty strings (""), null, undefined, false and the numbers 0 and NaN – Alexandr Jun 22 '16 at 12:45
  • sorry,i can't mark this as best answer due to less reputations. you should also see my answer. something new that i found i.e.using Boolean() function. – Srishti Sharma Jun 22 '16 at 13:47
  • @srishtisharma now you have enough rep) – Alexandr Jun 22 '16 at 15:17
1

this is what i was looking for and absolutely logical-

for(var i in array){  
  if((prop in array[i])&& Boolean(array[i][prop]))  
  {  
  //do something  
  }   
}

the Boolean() function is something which made my day. Learn more at this link.

Alexandr
  • 5,460
  • 4
  • 40
  • 70
0

Look at the below example.

let the json object be

 var a = { obj1:"a",obj2:"b"}

to check if an object exists,you can use hasOwnProperty() method.

a.hasOwnProperty("obj2") //this will return true
a.hasOwnProperty("obj3") // this will return false

to check the value of an object

 if(a["obj1"] && a["obj1"]!="" && a["obj"]!=0){
  //place your logic here
}
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22
  • i have tried using that (hasOwnProperty()) but that didn't worked also.both worked the same way. – Srishti Sharma Jun 22 '16 at 12:21
  • can you please post the json object? so that i can give a try. – Sudharsan Selvaraj Jun 22 '16 at 12:27
  • ya sure, here -truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); this should return true. – Srishti Sharma Jun 22 '16 at 12:35
  • below code works fine for me, var a=[{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex":"female"}] for(i=0;i – Sudharsan Selvaraj Jun 22 '16 at 12:43
  • try this-truthCheck([{"single": "double"}, {"single": NaN}], "single"); P.S. the key may exist or not and all the "should not be present" is not responding correctly in this case.i have tried this earlier. – Srishti Sharma Jun 22 '16 at 12:46
  • Yeah it is working fine. [link](http://www.tiikoni.com/tis/view/?id=c9c1b7a) view my console output in the above link – Sudharsan Selvaraj Jun 22 '16 at 12:52
  • yes,it works! that was a silly mistake earlier and it works all fine with hasOwnProperty(). :) – Srishti Sharma Jun 22 '16 at 13:37
  • thanks and please see my answer also which is using Boolean() function. :) – Srishti Sharma Jun 22 '16 at 13:48