我有这段代码:
<script>
fbq('track', 'InitiateCheckout', {
content_ids: {{orderFormProducts}},
content_type: 'product'
});
</script>
这给了我整个阵列:
content_ids: [
{
"id":"1",
"name": "product name",
"sku": "10",
"skuName": "sku name"
},
{
"id": "2",
"name": "product name",
"sku":"20",
"skuName": "sku name"
}
]
我需要的是只得到该数组中所有对象的sku
值,如下所示:
content_ids: ["10", "20"]
如果我使用{{orderFromProducts}}[0].sku
,我可以从0索引对象中获取sku
值,但我需要像之前提到的数组内的所有对象,并且{{orderFromProducts}}.sku
不起作用
有什么想法吗?
答案 0 :(得分:0)
content_ids.map(el=>el.sku);
在你问之前请http://google.it,你可以找到 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/map
答案 1 :(得分:0)
使用array map。
orderFromProducts.map(function(product) {
return product.sku;
})
答案 2 :(得分:0)
您可以使用.map()
创建一个填充了sku
值的新数组。
let content_ids = [{
"id": "1",
"name": "product name",
"sku": "10",
"skuName": "sku name"
},
{
"id": "2",
"name": "product name",
"sku": "20",
"skuName": "sku name"
}
];
let skus = content_ids.map(x=>x.sku);
console.log(skus);
&#13;
对于IE 7等旧式浏览器,请改用for
。
var content_ids = [{
"id": "1",
"name": "product name",
"sku": "10",
"skuName": "sku name"
},
{
"id": "2",
"name": "product name",
"sku": "20",
"skuName": "sku name"
}
];
var skus = [];
for (var i = 0, len = content_ids.length; i < len; i++) {
skus.push(content_ids[i].sku);
}
console.log(skus);
&#13;