vendredi 29 mai 2015

Check if value exists in object and if not push it

I need to check if value exists in object and if not add the value in object. Eg:My Object is and I want to check on UserId parameter.

var myObj= [{'UserId':123,'Username':'abc'},{'UserId':567,'Username':'xyz'},{'UserId':890,'Username':'pqr'}];

Now suppose I want to add new element {'UserId':223,'Username':'mln'} , I should be allowed to , however if I try to add an element {'UserId':123,'Username':'hij'} I should be not.

It will check it dynamically for a number of elements and not just the example provided above.

How should do this with jquery/javascript.

Aucun commentaire:

Enregistrer un commentaire