Google Chrome: JavaScript associative arrays, evaluated out of sequence

前端 未结 7 2116
耶瑟儿~
耶瑟儿~ 2020-12-07 02:24

Ok, so on a web page, I\'ve got a JavaScript object which I\'m using as an associative array. This exists statically in a script block when the page loads:

v         


        
7条回答
  •  情歌与酒
    2020-12-07 02:30

    Think of an associative array as a paper sack into which all the key-value pairs are placed. As you reach your hand into the sack to look at the pairs (such as with the for...in loop), the order that you encounter them is for all practical purposes random.

    If you want to see them in a specific order, you'll need to extract the keys into an array and sort it. Then walk across the array, using the keys you encounter to index into the associative array.

提交回复
热议问题