如何从JS对象中访问值以在for循环中更改它?
var dataForModal = {gpsID: response.id, lastUpdated: response.lastUpdate};
$.each(dataForModal, function (index, value) {
if(value === undefined) {
//this will ofcourse just change the value of the local var value
value="<i class='mNotAvailable'>not available</i>";
}
});
答案 0 :(得分:1)
您只是将新值分配给本地范围内的变量,不会以任何方式影响对象,
$.each(dataForModal, function (index, value) {
if(value === undefined) {
dataForModal[index] ="<i class='mNotAvailable'>not available</i>";
//use dataForModal here and use the "index" to access the relevant key.
}
});