{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
我们如何更改约翰的姓氏?(不知道是Doe)
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
我们如何更改约翰的姓氏?(不知道是Doe)
您可以使用$.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;
}
}
不需要 jQuery:
for(var i in obj.employees)
{
if(obj.employees[i].firstName == 'John')
{
obj.employees[i].lastName = "newValue";
}
}