javascript - 如何在谷歌标签管理器中获取数组中对象的属性值
问题描述
我想获取数组中对象的属性值。
我将以下项目发送到 Google 跟踪代码管理器。
items: [{id: 'exampleX', price: '9999'},{id: 'exampleY', price: '9999'},{id: 'exampleZ', price: '9999'}]
除了使用自定义 JavaScript,还有其他方法可以获取 exampleX 的价格吗?
或者有没有更简单的获取方式?
此致,
解决方案
看一下这个:
const items = [{id: 'exampleX', price: '9999'},{id: 'exampleY', price: '9999'},{id: 'exampleZ', price: '9999'}]
const price = items.filter(e => e.id === "exampleX").pop()?.price;
console.log(price)
items.filter(e => e.id === "exampleX")
匹配每个元素id==="exampleX"
.pop()
从数组中获取第一个元素(如果没有找到元素,可能为 null)?.price
安全展开以获取房产价格(如果未找到任何元素,则未定义)
更新:
const items = [{id: 'exampleX', price: '9999'},{id: 'exampleY', price: '9999'},{id: 'exampleZ', price: '9999'}]
const price = items[0].price; // first element = index 0
console.log(price)
推荐阅读
- python - 如何将时间戳转换为 df 到 excel python?
- flutter - 为什么这个 AnimatedSwitcher 不对变化进行动画处理?
- c# - 计算除星期日外 2 天的小时数
- html - 尝试使用固定位置修复导航栏时导航栏位置发生变化
- tcl - Tcl如何对文本中的某些单词进行排序并取最后一个
- publish - 已在 Google 商店发布的应用无法通过 Google Assistant 的声音启动
- c++ - static_assert 计算非常量表达式
- opencv - ROS/OpenCV/Flask - 如何流式传输?
- c# - 当主体实体中的两个属性指向依赖实体中的相同属性时,如何使用 EF Core?
- node.js - 在express,node.js中获取完整的IP地址