0

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?

2
  • Is that raw return from Ajax, I suggest you should return in json format, easier to parse Commented Jul 5, 2012 at 17:44
  • Yes, return is in JSON format. This is only source copied from chrome developer tools. Commented Jul 5, 2012 at 17:53

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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
}

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.