database - JavaScript JSON Table Matching -


i've been going through hell trying figure out, i'm sure it's not big of deal, perhaps i'm tired coding night, use help...

"users" database-like object containing user account information ( server-side, of course ), "get" function returns array of matching objects users array.

var users = [     {         name:"xymon",                age:19,         pass:"mypass",           time:1364101200684     },     {         name:"test",                 age:19,         pass:"x",            time:1364101200993     },     {             name:"test",             age:19,             pass:"bleh",             time:1364101200992     }  ];  function get(a){  } 

what i'm wanting "get" return properties match specified object (a) in array, so...

var matching_users = get({name:"test",age:19}); 

this return 2 objects in "users" array because properties match specified properties in "get" "matching_users" return as...

[     {         name:"test",                 age:19,         pass:"x",time:1364101200993     },     {             name:"test",             age:19,             pass:"bleh",             time:1364101200992     }  ] 

iterate array , check each item matching:

function get(a){ var r = []; (var i=0, len=users.length; i<len; ++i) {     var doadd = true;     (var p in a) {         if (a.hasownproperty(p)) {             if (a[p] != users[i][p]) {                 doadd = false;                 break;             }         }                 }         if (doadd)         r.push(users[i]); } return r; } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -