首页 > 解决方案 > Object.keys(obj) 返回不在 obj 中的项目

问题描述

我正在尝试返回我的 obj 的所有键以在 Google Apps 脚本中创建标题行。这是我 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}

这就是我输出的内容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]

我得到了所有这些以 set 开头的额外键,并且 get 在 obj 中不存在,我不知道为什么。

标签: javascriptobjectgoogle-apps-scriptgoogle-tag-manager

解决方案


这些键存在于对象中,这就是为什么在使用 检索这些键时显示它们的原因Object.keys()console.log()在 Apps 脚本中尝试对对象进行字符串化(尽管由于应用程序脚本中的自定义实现而以一种奇怪/非标准的方式进行)并且stringify 排除了函数,因此当脚本直接使用console.log().

您可以遍历对象并过滤掉这些函数:

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

推荐阅读