Search code examples
javascriptarraysstringreturn-valuegoogle-tag-manager

Select value in string


I am new here. I want to get 'value' from string. In the code below, get the numbers after the colon: return 59.85

document.querySelector("div.column1.container > div.column1.content > script")

gtag('event', 'conversion', {
'send_to': 'AW-615429024/N2wOCNOMveYBEKDnuqUC',
'value': 59.85,
'currency': 'TRY',
'transaction_id': '08112020160736-MWO'
});

After colon but before the comma

enter image description here

enter image description here


Solution

  • I'm guessing you are using gtag.js. The third argument is the additional configuration options but I'm not sure why you are asking for the value since you are the one making the object for it.

    Anyway to access any object's keys you need to assign it to a variable like this:

    let obj = {
    'send_to': 'AW-615429024/N2wOCNOMveYBEKDnuqUC',
    'value': 59.85,
    'currency': 'TRY',
    'transaction_id': '08112020160736-MWO'
    }
    

    To access the key called value, just use obj.value . The following line should print the value in console.

    console.log(obj.value)
    

    Alternatively, you can use the following to access the 'value' key:

    ({
    'send_to': 'AW-615429024/N2wOCNOMveYBEKDnuqUC',
    'value': 59.85,
    'currency': 'TRY',
    'transaction_id': '08112020160736-MWO'
    }).value