0

I want to add data into an object, and my object contains nested data. Example data:

pageviewEvent {
  client: "clientName",
  page {
    document_referrer: 'some http refer header data goes here',
    window_height: 1920,
    window_width: 1200
  }
}

Some data is undefined or null and I do not want to add this undefined/null data into the object.

I made a function that works to add data to the object conditionally (if not undefined) but I can't figure out how to add data to nested objects in the function?

I could just make a bunch of if statements, but figure it's better to put the condition test into a function to reduce code size.

Example code with comment showing thinking of what I am trying but doesn't work:

//function to check if variable undefined or null, if not -> add to pageviewEvent arrayKey, variableName
function isUndefinedNull(arrayKey, variableName) {

    var evalVariableName = eval(variableName);

    if (evalVariableName !== undefined && evalVariableName !== null && evalVariableName !== "") {
        console.log(arrayKey);
        console.log(variableName);
        pageviewEvent[arrayKey] = evalVariableName;
        //array key could be nested, for instance pageview[page][title] or pageview.page.tile
    }
}

//make event array  
const pageviewEvent = { }

//add static data
pageviewEvent.client = 'neguse';

//if not null or undefined add data   
isUndefinedNull('referrer.full', 'document.referrer');
//want to put data into object pageviewEvent.referrer.full or pageviewEvent[referrer][full]

Thanks for any help. I feel like this answer can help but I can't figure it out.

glennsl
  • 28,186
  • 12
  • 57
  • 75
Dillon Doyle
  • 305
  • 3
  • 14
  • 1
    Why are you passing in a variable name and `eval`ing it? Can't you just pass the value in directly? – glennsl Sep 21 '17 at 23:33
  • 1
    This is a good side point - in general I would never, ever use `eval()` in javascript unless I had no other alternative. It's very unsafe, makes it hard to lint your code, and is generally forbidden in javascript guidelines. – Duncan Thacker Sep 21 '17 at 23:56
  • @glennsl I was trying to save space, 50+ lines to add into object. var a = some.html/dom.value; many times. – Dillon Doyle Sep 22 '17 at 01:53
  • Good point @duncan-thacker to think about -> probably not worth any gains to avoid xss problems etc.Would be nice to figure out a safe concise function – Dillon Doyle Sep 22 '17 at 01:54
  • @DillonDoyle your explanation makes no sense - just pass the variable by value! – Duncan Thacker Sep 22 '17 at 07:16
  • @duncan-thacker I prob am not understanding/wrong. sorry for edits writing back in comments isn't great: the problem is/was if there is no value it throws not defined error, and checking if undefined is what I was trying to put into a function to stop repeating. there's got to be a correct/better way, but i cant figure it out hence the questions! `function echoMe(arrayKey,variableName) { console.log(arrayKey); if(variableName!==undefined && evalVariableName!==null && evalVariableName!=="") { console.log(variableName); } } echoMe('key', document.referrer);` – Dillon Doyle Sep 22 '17 at 07:56
  • Don't pass in `"document.referrer"` and then `eval()`, just pass in `document.referrer`. – Duncan Thacker Sep 22 '17 at 12:09
  • @DuncanThacker the problem is when there is no data (try example code in my comment above on a blank page with no document referrer) which is why I was trying to put the check for undefined into a function to remove repetitive code – Dillon Doyle Sep 23 '17 at 01:19

1 Answers1

1

I recommend using the lodash function _.set(), documentation can be found here: https://lodash.com/docs/4.17.4#set

_.set( pageviewEvent, "referrer.full", "some-value" );

If you want to customise the behaviour of how nesting is handled when there's an undefined value, you can instead use _.setWith() - see https://lodash.com/docs/4.17.4#setWith

Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20