Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Order of keys in expanded vs collapsed view in Chrome console

While working on another problem, I was misled by the fact that Chrome (and perhaps other browsers) somehow magically sorts keys of an object when outputting it to the console:

let inputs = ["XMl", "SFJ", "dTg", "amN"];
let promises = {};

for (let val of inputs) {
   let p = new Promise(
      (resolve) => setTimeout(resolve, 500 + Math.random() * 1500, "new:" + val)
   );
   promises[val] = p
}

console.log(promises)
Promise.allSettled(Object.values(promises)).then((res) => console.log(res))

Now the browser (Chrome) shows me:

enter image description here

So, there is a difference between the expanded and the collapsed output. Is that standard behavior? Where does this come from?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's a simpler replication of the issue:

const obj = {
    XMl: 1,
    SFJ: 1,
    dTg: 1,
    amN: 1,
};
console.log(obj);

For which I get:

devtools object screenshot with the order described below

The summary seems to be shown in property order (more on that in a moment), but then when displaying the object details Chrome's devtools shows the properties in alphabetical order (including grouping capitals together; it looks very much like the ordering simple < and > would do, rather than localeCompare or similar). Firefox's looks similar.

The first is covered by the JavaScript specification (details here). I don't think there's any standard that covers the second, but if you think about it, alpha order is probably more useful than property order when you're trying to find a property in an object with lots of properties. Property order is complicated and only mostly based on the order in which the properties were created, so it can can seem chaotic. And grouping the capitals together categorizes slightly, given the JavaScript convention that initial capital letters are almost entirely reserved for constructor functions and all caps constants.


Originally you used the term "associative array" instead of object. As I mentioned in a comment, they're really objects, not just associative arrays. If you did want just a basic "map" style name/value pairs thing, that would be a Map and the order of map entries is nice and simple: It's the order the entry was created. If we do the same thing we did above but with a Map, both the summary and the detail show the information in the same order:

const map = new Map([
    ["XMl", 1],
    ["SFJ", 1],
    ["dTg", 1],
    ["amN", 1],
]);
console.log(map);

devtools map object screenshot showing same order in summary and detail

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda