George
George

Reputation: 37

Object.keys(obj) returning items not in obj

I'm trying to return all the keys of my obj to create a header row in Google Apps Script. This is what is returned when I console.log(obj):

{tagId=270, accountId=XXXXX, monitoringMetadata={type=map}, firingTriggerId=[18], type=sp, containerId=XXXXX, workspaceId=XXXXX, name=GADR - Product Detail View - Product Detail Page, tagManagerUrl=https://tagmanager.google.com/#/container/accounts/XXX/containers/XXX/workspaces/XXX/tags/270?apiLink=tag, parameter=[{value={{CJ - GADR SKU - Product Detail Page}}, type=template, key=eventItems}, {type=template, key=eventValue, value={{DLV - Product Price - Product Detail Pages}}}, {value=true, key=enableDynamicRemarketing, type=boolean}, {type=list, key=customParams, list=[{map=[{type=template, key=key, value=product_name}, {key=value, type=template, value={{DLV - Product Name - Product Detail Pages}}}], type=map}, {type=map, map=[{value=product_price, type=template, key=key}, {key=value, type=template, value={{DLV - Product Price - Product Detail Pages}}}]}, {map=[{type=template, key=key, value=product_id}, {value={{DLV - Product ID - Product Detail Pages}}, type=template, key=value}], type=map}, {map=[{value=product_category, type=template, key=key}, {value={{DLV - Product Category - Product Detail Pages}}, key=value, type=template}], type=map}, {type=map, map=[{value=user_id, key=key, type=template}, {value={{DLV - User ID - All Pages}}, type=template, key=value}]}, {map=[{value=user_status, type=template, key=key}, {type=template, value={{DLV - User Status - All Pages}}, key=value}], type=map}, {type=map, map=[{value=page_type, type=template, key=key}, {key=value, value={{DLV - Page Type - All Pages}}, type=template}]}, {map=[{type=template, value=user_purchase_count, key=key}, {key=value, type=template, value={{DLV - User Purchase Count - All Pages}}}], type=map}, {type=map, map=[{value=user_lifetime_value, type=template, key=key}, {key=value, value={{DLV - User Lifetime Value - All Pages}}, type=template}]}]}, {type=template, value=view_item, key=eventName}, {type=template, value={{CST - Google Ads ID - All Pages}}, key=conversionId}, {type=template, value=USER_SPECIFIED, key=customParamsFormat}, {key=rdp, value=false, type=boolean}], tagFiringOption=oncePerEvent, path=accounts/XXX/containers/XXXX/workspaces/XXXX/tags/270, fingerprint=1608071670124}

And this is what is outputted when I console.log(Object.keys(obj));

[setMonitoringMetadataTagNameKey, setName, setTagFiringOption, setNotes, getMonitoringMetadata, tagId, firingTriggerId, getSetupTag, getFiringTriggerId, getName, name, getTagFiringOption, getParentFolderId, setWorkspaceId, containerId, getFingerprint, getTagManagerUrl, parameter, getBlockingTriggerId, setLiveOnly, setFingerprint, setTagManagerUrl, getContainerId, setScheduleEndMs, setTagId, getPaused, path, setContainerId, getLiveOnly, setFiringTriggerId, getBlockingRuleId, getFiringRuleId, getTagId, setTeardownTag, getTeardownTag, setFiringRuleId, getType, getPriority, accountId, setPaused, monitoringMetadata, setScheduleStartMs, getNotes, setParameter, toString, setParentFolderId, getPath, setSetupTag, fingerprint, setAccountId, getAccountId, workspaceId, setBlockingTriggerId, type, tagFiringOption, tagManagerUrl, getMonitoringMetadataTagNameKey, setPriority, setType, getScheduleEndMs, setBlockingRuleId, setPath, getParameter, getWorkspaceId, getScheduleStartMs, setMonitoringMetadata]

I'm getting all of these extra keys that start with set and get that don't exists in the obj and I can't figure out why.

Upvotes: 0

Views: 80

Answers (1)

TheMaster
TheMaster

Reputation: 50765

Those keys exists in object and that's why they're shown, when those keys are retrieved using Object.keys(). console.log() in Apps Script attempts to stringify the object(albeit in a weird/non-standard fashion due to custom implementation in apps script) and stringify excludes functions and therefore they're are not shown when the script directly logs using console.log().

You can iterate over the object and filter those functions out:

const keysWithoutFunctions = Object.keys(obj).filter(key => typeof obj[key] !== 'function')

Upvotes: 4

Related Questions