{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
我们怎样才能改变约翰的姓氏? (不知道它是Doe)
答案 0 :(得分:3)
您可以使用$.each
效用函数:
$.each(obj.employees, function(i, v){
if (v.firstName === 'John') {
v.lastName = 'newValue'
// return false
}
})
您还可以使用原生for
循环。
for (var i = 0; i < obj.employees.length; i++) {
if (obj.employees[i].firstName === 'John') {
obj.employees[i].lastName = 'newValue';
return false;
}
}
答案 1 :(得分:1)
不需要jQuery:
for(var i in obj.employees)
{
if(obj.employees[i].firstName == 'John')
{
obj.employees[i].lastName = "newValue";
}
}