Return 不带引号的数组
Return array without quotes
场景
我想将产品 GTIN 推送到 Google Merchant Center。
我得到了什么
"products": ['{"gtin":"5704378978422"}', '{"gtin":"5704378978057"}']
我想得到什么
格式中的每个数组成员都没有单引号:
"products": [{"gtin":"GTIN1"}, {"gtin":"GTIN2"}]
如此处第 3 步所述:https://support.google.com/merchants/answer/7519329
我的 Google 跟踪代码管理器设置
- 我已经从 ecommerce.purchase.products
创建了一个数据层变量 {{DLV - ecommerce.purchase.products}}
- 我使用以下代码创建了自定义 Javascript 变量:
代码:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.reduce(function(arr, prod) {
return arr.concat("{" + '"' + "gtin" + '"' + ":" + '"' + prod.gtin + '"' + "}" ); }, []);
}
最好的方法是什么
我是 JavaScript 的新手,如果有任何意见和建议,我将不胜感激
通过使用 concat
函数,您创建的是字符串连接,而不是对象。
您可能应该使用 map
而不是 reduce
。像这样:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.map(function(prod) {
return {"gtin": prod.gtin};
});
}
你是说你想要这个吗?
const product = ['{"gtin":"5704378978422"}', '{"gtin":"5704378978057"}']
const newProduct = product.map(row => JSON.parse(row))
console.log(newProduct)
场景
我想将产品 GTIN 推送到 Google Merchant Center。
我得到了什么
"products": ['{"gtin":"5704378978422"}', '{"gtin":"5704378978057"}']
我想得到什么
格式中的每个数组成员都没有单引号:
"products": [{"gtin":"GTIN1"}, {"gtin":"GTIN2"}]
如此处第 3 步所述:https://support.google.com/merchants/answer/7519329
我的 Google 跟踪代码管理器设置
- 我已经从 ecommerce.purchase.products 创建了一个数据层变量 {{DLV - ecommerce.purchase.products}}
- 我使用以下代码创建了自定义 Javascript 变量:
代码:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.reduce(function(arr, prod) {
return arr.concat("{" + '"' + "gtin" + '"' + ":" + '"' + prod.gtin + '"' + "}" ); }, []);
}
最好的方法是什么
我是 JavaScript 的新手,如果有任何意见和建议,我将不胜感激
通过使用 concat
函数,您创建的是字符串连接,而不是对象。
您可能应该使用 map
而不是 reduce
。像这样:
function() {
var products = {{DLV - ecommerce.purchase.products}};
return products.map(function(prod) {
return {"gtin": prod.gtin};
});
}
你是说你想要这个吗?
const product = ['{"gtin":"5704378978422"}', '{"gtin":"5704378978057"}']
const newProduct = product.map(row => JSON.parse(row))
console.log(newProduct)