Chrome re-ordering object keys if numerics, is that normal/expected

后端 未结 7 1493

I noticed that certain code that evaluates some shoe sizes for an e-commerce site and outputs them on screen is messing up the order in Chrome.

JSON given can be:

相关标签:
7条回答
  • 2020-12-03 07:39

    My keys were too important for me to convert them to strings. Instead I created another array which just maintained the order of the keys.

    <?php 
    $origArray = valueReturnedFromFunction();
    $preservedOrder = array_keys($origArray);
    ?>
    <script>
    var origArray = <?php echo json_encode($origArray)?>;
    var preservedOrder = <?php echo json_encode($preservedOrder )?>;
    
    for(i in preservedOrder){
        var item = origArray[i];
        ...
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题