简体   繁体   中英

How can I force Firebug to print my JSON properties in the order they are declared?

I'm building JSON with JavaScript. Here's my object:

catalogoJSON = {
    condicion: condicionCatalogos0,
    tipo: tipoCatalogos0,
    idCatalogo: idCatalogo0,
    valor: valorCatalogos0
};

But when I print it with Firebug, I get my properties printed alphabetically like this:

{
    condicion: condicionCatalogos0,
    idCatalogo: idCatalogo0,
    tipo: tipoCatalogos0,
    valor: valorCatalogos0
}

Is there a way in JavaScript to get back my JSON with its properties in the order that I declare them, without having to change my properties' names?

Objects in JavaScript have no inherit order. Firebug just prints then alphabetically, because it feels like it (Chrome's dev tools to that too).

You can try to loop through the object, and print it yourself, that may keep the order.

for(var x in catalogoJSON){
    console.log(x, catalogoJSON[x]);
}

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