gpt4 book ai didi

javascript - 迭代 JS 对象元素并更改它们

转载 作者:行者123 更新时间:2023-12-03 07:11:26 25 4
gpt4 key购买 nike

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

最佳答案

您只是将新值分配给本地范围内的变量,这不会以任何方式影响对象,

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

关于javascript - 迭代 JS 对象元素并更改它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com