简体   繁体   中英

Better referencing syntax for nested objects?

I could not find anything online, but I'm curious if there's a more concise syntax for the following example:

if ( obj[a][b][c] != 0 ) {
    obj[a][b][c] = ( obj[a][b][c] / 2 ) + obj[a][b][c] + 100;
    obj[x][y][z] = obj[a][b][c];
}

I know for the times that I'm working with the actual value I could just cache it in a temporary variable, but I'm more interested in replacing the "obj[...] = " part with something more concise.

Well you could extract a variable and as @Jonas W noted in the comments you can combine assigments:

var cellP = obj[a][b];
var cell = cellP[c];
if ( cell != 0 ) {
    obj[x][y][z] = cellP[c] = 1.5 * cell + 100;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM