0

I have an object that I populate with with very long keys (strings that are around 400 chars long). When I try to print the object

console.log(Object.keys(myCoolObject))

or

console.log(myCoolObject)

I can only see TWO of the keys (when in fact I should see around 20 of them). The keys are there though, because I when I try to access the data in the object with any of the keys, it does work.

console.log(myCoolObject['aVeryVeryLongKeyHere'])

Does anyone know what is going on?

Dr.Boomerang
  • 87
  • 1
  • 1
  • 7
  • `myCoolObject('aVeryVeryLongKeyHere')` will try to execute the object like a function, it will not fetch the property `aVeryVeryLongKeyHere` from it. You need [bracket notation](https://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string/) – VLAZ Jan 06 '20 at 07:59
  • @VLAZ I do use bracket notation, I used the round brackets in the code snippet in the question by mistake. That is not the issue though - the problem is to see all the keys inside the object, not to access their values. – Dr.Boomerang Jan 06 '20 at 08:03
  • 1
    [I can't reproduce this](https://jsbin.com/taxehakiye/edit?js,console) – VLAZ Jan 06 '20 at 08:09
  • 3
    Are you filling in your object within an async function ? – Weedoze Jan 06 '20 at 08:13
  • 1
    Maybe the keys on the objects's `[[Prototype]]`? Try using this function: https://stackoverflow.com/a/30158566/3082296 – adiga Jan 06 '20 at 08:15
  • are you trying this in nodejs or in the brwoser? – fubar Jan 06 '20 at 08:21

2 Answers2

0

Why are you using long keys? The longer the keys the longer the lookup will take when the object grows and grows.

But that's besides the point. If you want to print very long keys I suggest you modify your approach.

The "native" logging doesn't show all the data you need/want because it truncates the keys as they go "off screen". So you need to iterate manually through the object with the long keys, then print out the key, then print out the value.

Luckily you can work with comma's in console.log, so you can chain multiple debugging things in one go. If you look at the example below, you see I first print the key name as string, then I print the value with the native console.log printing, not losing the possibility to drill down into the value.

Press look at the display in the browsers console(press f12).

var key, CoolObject = {
'BysodelightofshowingneitherbelievehepresentDealsighupinshewawaywhenPursuitexpressnoorpreparerepliedWhollyformedoldlatterfuturebutwaysheDayherlikewisesmallestexpensesjudgmentbuildingmancarriagegayConsideredintroducedthemselvesmrtodiscretionatMeansamongsawhopesforDeathmirthinohlearnheequalonBysodelightofshowingneitherbelievehepresentDealsighupinshewawaywhenPursuitexpressnoorpreparerepliedWhollyformedoldlatterfuturebutwaysheDayherlikewisesmallestexpensesjudgmentbuildingmancarriagegayConsideredintroducedthemselvesmrtodiscretionatMeansamongsawhopesforDeathmirthinohlearnheequalonBysodelightofshowingneitherbelievehepresentDealsighupinshewawaywhenPursuitexpressnoorpreparerepliedWhollyformedoldlatterfuturebutwaysheDayherlikewisesmallestexpensesjudgmentbuildingmancarriagegayConsideredintroducedthemselvesmrtodiscretionatMeansamongsawhopesforDeathmirthinohlearnheequalon' : { 'hello' : 'hi'},
'AffrontingimprudencedoheheeverythingSexlasteddinnerwantedindeedwishedoutlawFaradvancedsettlingsayfinishedrailleryOfferedchieflyfartherofmynocolonelshynessSuchonhelpyesomedoorifinLaughterproposallaughinganysonlawconsiderNeededexceptuppiquedanAffrontingimprudencedoheheeverythingSexlasteddinnerwantedindeedwishedoutlawFaradvancedsettlingsayfinishedrailleryOfferedchieflyfartherofmynocolonelshynessSuchonhelpyesomedoorifinLaughterproposallaughinganysonlawconsiderNeededexceptuppiquedanAffrontingimprudencedoheheeverythingSexlasteddinnerwantedindeedwishedoutlawFaradvancedsettlingsayfinishedrailleryOfferedchieflyfartherofmynocolonelshynessSuchonhelpyesomedoorifinLaughterproposallaughinganysonlawconsiderNeededexceptuppiquedanAffrontingimprudencedoheheeverythingSexlasteddinnerwantedindeedwishedoutlawFaradvancedsettlingsayfinishedrailleryOfferedchieflyfartherofmynocolonelshynessSuchonhelpyesomedoorifinLaughterproposallaughinganysonlawconsiderNeededexceptuppiquedan' : {'world':'planet'},
'HeshareoffirsttoworseWeddingsandanyopinionssuitablesmallestnayMyhehousesormonthssettleremoveladiesappearEngrossedsufferingsupposingherecommenddoeagernessCommandednoofdependingextremityrecommendattentiontolerablyBringinghimsmallestmetfewnowreturnedsurpriselearningjenningsObjectiondeliveredeagernessheexquisiteatdoinWarmlyuphenearermrmerelymeHeshareoffirsttoworseWeddingsandanyopinionssuitablesmallestnayMyhehousesormonthssettleremoveladiesappearEngrossedsufferingsupposingherecommenddoeagernessCommandednoofdependingextremityrecommendattentiontolerablyBringinghimsmallestmetfewnowreturnedsurpriselearningjenningsObjectiondeliveredeagernessheexquisiteatdoinWarmlyuphenearermrmerelymeHeshareoffirsttoworseWeddingsandanyopinionssuitablesmallestnayMyhehousesormonthssettleremoveladiesappearEngrossedsufferingsupposingherecommenddoeagernessCommandednoofdependingextremityrecommendattentiontolerablyBringinghimsmallestmetfewnowreturnedsurpriselearningjenningsObjectiondeliveredeagernessheexquisiteatdoinWarmlyuphenearermrmerelymeHeshareoffirsttoworseWeddingsandanyopinionssuitablesmallestnayMyhehousesormonthssettleremoveladiesappearEngrossedsufferingsupposingherecommenddoeagernessCommandednoofdependingextremityrecommendattentiontolerablyBringinghimsmallestmetfewnowreturnedsurpriselearningjenningsObjectiondeliveredeagernessheexquisiteatdoinWarmlyuphenearermrmerelyme': {'and':'or'},
'SighviewamhighneathalftowhatSentlateheldthansetwhywifeourIfanblessingbuildingsteepestAgreementdistrustsmrssixaffectionsatisfied.DayblushesvisitorendcompanyoldpreventchapterConsiderdeclaredoutexpensesherconcernsNoatindulgenceconvictionparticularunsatiableboisterousdiscretionDirectenoughoffotherssayeldestmayexetershePossibleallignorantsuppliedgetsettlingmarriagerecurredSighviewamhighneathalftowhatSentlateheldthansetwhywifeourIfanblessingbuildingsteepestAgreementdistrustsmrssixaffectionsatisfied.DayblushesvisitorendcompanyoldpreventchapterConsiderdeclaredoutexpensesherconcernsNoatindulgenceconvictionparticularunsatiableboisterousdiscretionDirectenoughoffotherssayeldestmayexetershePossibleallignorantsuppliedgetsettlingmarriagerecurredSighviewamhighneathalftowhatSentlateheldthansetwhywifeourIfanblessingbuildingsteepestAgreementdistrustsmrssixaffectionsatisfied.DayblushesvisitorendcompanyoldpreventchapterConsiderdeclaredoutexpensesherconcernsNoatindulgenceconvictionparticularunsatiableboisterousdiscretionDirectenoughoffotherssayeldestmayexetershePossibleallignorantsuppliedgetsettlingmarriagerecurredSighviewamhighneathalftowhatSentlateheldthansetwhywifeourIfanblessingbuildingsteepestAgreementdistrustsmrssixaffectionsatisfied.DayblushesvisitorendcompanyoldpreventchapterConsiderdeclaredoutexpensesherconcernsNoatindulgenceconvictionparticularunsatiableboisterousdiscretionDirectenoughoffotherssayeldestmayexetershePossibleallignorantsuppliedgetsettlingmarriagerecurred' :{'universe':'galaxies'}
}
console.log('truncated keys because they are too long for display');
console.log(CoolObject);
console.log('truncated keys because they are too long for display');
console.table(CoolObject);

console.log('Showing by iterating the keys');
for(key in CoolObject) {
   if(CoolObject.hasOwnProperty(key)) {
      console.log(key,':', CoolObject[key]);
   }
}
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

Ok so the problem didn't have anything with long keys, but as @Weedoze guessed (Thanks!) I was filling the object inside an async function, and since I wrote my console.log inside this function, I was seeing the object before it received all the data.

Dr.Boomerang
  • 87
  • 1
  • 1
  • 7