Show original order of object properties in console.log

后端 未结 4 1622
忘了有多久
忘了有多久 2020-12-10 13:06

I need for some debugging to see the original order of one JavaScript object\'s properties but (at least in chrome devtools) console.log() shows me an alphabeti

4条回答
  •  没有蜡笔的小新
    2020-12-10 13:59

    console.log does indeed sort the properties, in some cases you can use JSON.stringify which preserves the order, e.g.

    console.log(JSON.stringify(obj, null /*replacer function */, 4 /* space */))
    

    NB: contrary to the popular belief, js objects maintain the enumeration order, as per the OwnPropertyKeys specification (integers first, then other properties in insertion order)

提交回复
热议问题