迭代JS Object Elements并改变它们

时间:2016-04-13 15:37:00

标签: javascript

如何从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>";
        }
});

1 个答案:

答案 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.
  }
});