3 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
最簡(jiǎn)單的方法是創(chuàng)建一個(gè)函數(shù):
var obj = {
a: {
b: 1
}
};
function increment(value, max) {
// this do the same as your ternary operator
return (value + 1) % max;
}
obj['a']['b'] = increment(obj['a']['b'], 100);
console.log(obj['a']['b']);
// THIS DOES NOT WORK (won't update obj):
var x = obj['a']['b'];
x = increment(x, 100);
console.log(obj['a']['b'], x);

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
( % )像這樣使用模運(yùn)算符 -
var age = 95;
var limit = 100;
function incrementValue()
{
age = (age+1) % limit;
document.getElementById('number').value = age;
}
document.getElementById('number').value = age;
<form>
<input type="number" id="number"/>
<input type="button" onclick="incrementValue()" value="Increment Value" />
</form>

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
將temp變量存儲(chǔ)到變量中,然后執(zhí)行操作:
試試這個(gè):
let age = obj[layer01][gridY][gridX][shape];
obj[layer01][gridY][gridX][shape] = ( age < 99) ? (age + 1) : 0;
添加回答
舉報(bào)