0

I have an array of objects

object will be in below format

var newUserDetail={"Age":"21","name":"Vicky","UserId":"198303"};

Now I am trying to compare UserId and replace the values

//usersList contains array of newUserDetail kind of objects

jQuery(usersList).each(function(){
if(this.UserId==newUserDetail.UserId){
this=newUserDetail;
}
});

But it throws an error

Invalid left-hand side in assignment
2
  • sorry! check the edited question Commented Jun 14, 2015 at 11:30
  • i think it's probably because "this" inside the if condition, and why this=newUserDetail; ? Commented Jun 14, 2015 at 11:31

2 Answers 2

1

Set the array entry:

jQuery(usersList).each(function (i) {
    if (this.UserId == newUserDetail.UserId) {
        usersList[i] = newUserDetail;
        return false; //if you want to break the loop
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$(document).ready(function() {
  var newUserDetail = {
    "Age": "21",
    "name": "Vicky",
    "UserId": "198303"
  };

  var tm = {
    "Age": "21",
    "name": "Vicky",
    "UserId": "198303"
  };
  $.each(newUserDetail, function(k, i) {
    if (i == tm.UserId) {
      alert("User ID match");
    }


  })



})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.