Koga
Koga

Reputation: 135

Javascript object check

I have this object from AJAX returned:

reseeds: [,…]
0: {id:1, t:13447, userid:1, time:2012-07-04 19:07:54, username:x, userlevel:8, donor:no,}
1: {id:2, t:13447, userid:2, time:2012-07-04 09:04:27, username:y, userlevel:0, donor:no,}
2: {id:3, t:13447, userid:3, time:2012-07-04 09:04:30, username:z, userlevel:0, donor:no,}
3: {id:4, t:13447, userid:4, time:2012-07-04 09:04:35, username:w, userlevel:0, donor:no,}

And I need to check if this object contains in "userid" some value.
I.E.:
I have value = 2, so how can I check if one of object arrays in userid == 2?? I need to return TRUE if any of userid == 2, and FALSE if no. If value = 5, i need tor return FALSE, because no one of userid in object contains userid = 5. Is there some function for it or I need to write my own for cycle?

Upvotes: 0

Views: 78

Answers (2)

Koga
Koga

Reputation: 135

I do it with this:

var user_data = _return[idx+'s'];
for(var i=0;i<user_data.length;i++)
{
  if(user_data[i]['userid'] == _returnX['datas']['userid'])
  {
    var add = false;
  }
}

if(add === undefined)
{
  user_data[user_data.length] = _returnX['datas'];
  // do some stuff
}

Upvotes: 0

Engineer
Engineer

Reputation: 48793

There is a Array.some function you could use:

//generic parameter for 'Array.some' function
function useridValidator( userid ){
    return function(item){
        return item.userid === userid;
    };
}
console.log( ajaxData.reseeds.some( useridValidator(1) ) );   //true
console.log( ajaxData.reseeds.some( useridValidator(5) ) );   //false

Upvotes: 1

Related Questions