Me gustaría saber la función correcta para Google Tag Manager, cuando crea un JavaScript personalizado, ¿cómo puedo obtener el producto "id" ("259496742","1061024") cuando tiene una matriz?
{ "0": "event", "1": "purchase", "2": { "transaction_id": "ORD00174", "affiliation": "Lightspeed", "value": "0.00", "currency": "EUR", "tax": "0.00", "shipping": "0.00", "items": [ { "id": 259496742, "name": "Haarklem Print", "currency": "EUR", "brand": "", "variant": 259496742, "price": "9.95", "quantity": 1 }, { "id": 1061024, "name": "Lila Hoed", "currency": "EUR", "brand": "", "variant": 263376478, "price": "19.95", "quantity": 1 } ],Tu puedes hacer:
const obj = { "0": "event", "1": "purchase", "2": { "transaction_id": "ORD00174", "affiliation": "Lightspeed", "value": "0.00", "currency": "EUR", "tax": "0.00", "shipping": "0.00", "items": [ { "id": 259496742, "name": "Haarklem Print", "currency": "EUR", "brand": "", "variant": 259496742, "price": "9.95", "quantity": 1 }, { "id": "1061024", "name": "Lila Hoed", "currency": "EUR", "brand": "", "variant": 263376478, "price": "19.95", "quantity": 1 } ], } } let ids = [] for(const key in obj) { const currentValue = obj[key] if (Object.prototype.hasOwnProperty.call(currentValue, 'items')) { ids = [...ids, ...currentValue.items.map(item => item.id)] } } console.log(ids)