javascript - 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 中不存在,我不知道为什么。
解决方案
这些键存在于对象中,这就是为什么在使用 检索这些键时显示它们的原因Object.keys()
。console.log()
在 Apps 脚本中尝试对对象进行字符串化(尽管由于应用程序脚本中的自定义实现而以一种奇怪/非标准的方式进行)并且stringify 排除了函数,因此当脚本直接使用console.log()
.
您可以遍历对象并过滤掉这些函数:
const keysWithoutFunctions = Object.keys(obj).filter(key => typeof obj[key] !== 'function')
推荐阅读
- angular - Ionic 使用 IdentityServer4 作为 IDP
- r - R 因子设计 AOV、ANOVA 错误的交互自由度
- java - 以下格式的 Json 到 csv
- amazon-ses - SES 在电子邮件正文中发送 base64 字符(而不是 html)[基本案例]
- wpf - Microsoft.Win32.SystemEvents 事件不适用于 WeakEventManager
- javascript - d3.js 在两种十六进制颜色之间进行插值
- robotframework - 如何在表格列上循环?
- c# - 在首次加载之前通过授权服务器保护 SPA
- wordpress - AWS 上的 Wordpress 多站点服务器水平扩展
- php - 如何使用 PHP 运行 python 脚本并创建文件?