在jquery中为对象数组中的对象赋值

时间:2015-06-14 11:24:47

标签: javascript jquery

我有一个对象数组

对象将采用以下格式

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

现在我正在尝试比较UserId并替换值

//usersList contains array of newUserDetail kind of objects

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

但它会抛出错误

Invalid left-hand side in assignment

2 个答案:

答案 0 :(得分:1)

设置数组条目:

jQuery(usersList).each(function (i) {
    if (this.UserId == newUserDetail.UserId) {
        usersList[i] = newUserDetail;
        return false; //if you want to break the loop
    }
});

答案 1 :(得分:1)

试试这个



char[]

$(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");
    }


  })



})