0

First this is what my urlObject looks like:

{
    term_id_1: "2155"
    term_id_2: "2894"
    ticker_1: "SPY"
    ticker_2: "SPY"
}

I can have up to 3 terms and tickers so like:

ticker_1, ticker_2, ticker_3

With their matching tags:

term_id_1, term_id_2, term_id_3

Here is my current function that checks if the ticker and tag combinations exists then calls another function:

function rebuildContainer(urlObject) {
    console.log('urlObject',urlObject);

    if (urlObject.ticker_1 && urlObject.term_id_1) {
        var tickerObj = {};
            tickerObj.ticker = urlObject.ticker_1;
        var tagObj = {};
            tagObj.term_id = urlObject.term_id_1;
        saveTickerTags(tickerObj, tagObj);
    }

    if (urlObject.ticker_2 && urlObject.term_id_2) {
        var tickerObj = {};
            tickerObj.ticker = urlObject.ticker_2;
        var tagObj = {};
            tagObj.term_id = urlObject.term_id_2;
        saveTickerTags(tickerObj, tagObj);
    }

    if (urlObject.ticker_3 && urlObject.term_id_3) {
        var tickerObj = {};
            tickerObj.ticker = urlObject.ticker_3;
        var tagObj = {};
            tagObj.term_id = urlObject.term_id_3;
        saveTickerTags(tickerObj, tagObj);
    }
}

This feels like it could be easily simplified, how would you do it?

vinayakj
  • 5,591
  • 3
  • 28
  • 48
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

5 Answers5

1

Haven't really tested much, but this should work so that you won't have to hardcode the id...

function rebuildContainer(urlObject) {
    // get the keys that contain the 'ticker' text
    Object.keys(urlObject).filter(function (k) {
        return k.indexOf('ticker') > -1;
    }).forEach(function (k) {
        // get the number at the end and look it up on the urlObject
        var termId = urlObject['term_id_' + k.match(/\d+$/)[0]];
        // if it is defined, save the object
        if(termId !== undefined) {
            var tickerObj = {};
            var tagObj = {};
            tickerObj.ticker = urlObject[k];
            tagObj.term_id = termId;
            saveTickerTags(tickerObj, tagObj);
        }
    });
}
brbcoding
  • 13,378
  • 2
  • 37
  • 51
  • Thx! This is the kind of solution I was looking for, to dynamically call a forEach type loop... Will test it out later – Leon Gaban Oct 29 '15 at 22:26
  • Hmm did not get this to work as plugged in... may try to play with it a bit more – Leon Gaban Oct 30 '15 at 03:17
  • @LeonGaban what's not working? **[Here's a running demo](http://jsbin.com/ponaxek/edit?js,console)**. – brbcoding Oct 30 '15 at 16:39
  • Thanks for the working Demo! Hmm... I feel that this function is harder to read, but it fixes the problem of having repeated / unused code. – Leon Gaban Oct 30 '15 at 17:19
0

Make a function for it that takes a ticker and a term_id and call it three times.

If you feel adventurous, write a loop from 1 to 3 that looks up the properties dynamically.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

create a function which accepts test conditions and call it.

function rebuildContainer(urlObject) {
    console.log('urlObject',urlObject);
    var keyPairs = [['ticker_1', 'term_id_1'], 
               ['ticker_2', 'term_id_2'], ['ticker_3', 'term_id_3']]

    for(var i=0; i<keyPairs.length;i++){
      if(ifExists(keyPairs[i][0], keyPairs[i][1])) {
        var tickerObj = {ticker : urlObject.ticker_1};
        var tagObj = {term_id : urlObject.term_id_1};
        saveTickerTags(tickerObj, tagObj);
      }
    }
}
function ifExists(tic,term){
  return (urlObject[tic] && urlObject[term])
}

Or If ticker_1, term_id_1 are always in this kinda of form then

function rebuildContainer(urlObject) {
    console.log('urlObject',urlObject);

    for(var i=1; i<=(Object.keys(urlObject).length)/2;i++){
      if(urlObject["ticker_"+i] && urlObject["term_id_"+i]) {
        var tickerObj = {ticker : urlObject.ticker_1};
        var tagObj = {term_id : urlObject.term_id_1};
        saveTickerTags(tickerObj, tagObj);
      }
    }
}
vinayakj
  • 5,591
  • 3
  • 28
  • 48
0

Here is an implementation of how the already mentioned function could look like:

function rebuildContainer(urlObj) {
    function checkAndSaveTicker(n) {
        if(urlObj.hasOwnProperty('ticker_'+n) &&   urlObj.hasOwnProperty('term_id_'+n)){
            saveTickerTags({'ticker' : urlObj['ticker_'+n]},
                           {'term_id' : urlObj['term_id_' + n]});
        }
    }
    checkAndSaveTicker(1);
    checkAndSaveTicker(2);
    checkAndSaveTicker(3);
}
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
vkluge
  • 324
  • 3
  • 11
0

Note, I posting this Answer here, because this is the solution I eventually went with.

function rebuildContainer(urlObject) {
    var deferred = $q.defer();
    var termIds  = ['term_id_1', 'term_id_2', 'term_id_3'];

    var filteredTermIds = _.filter(termIds, function(termId) {
        return urlObject[termId];
    });

    _.each(filteredTermIds, function(termId, index) {
        ApiFactory.getTagDataSilm(urlObject[termId]).then(function(res) {
            var tickerObj = {}, tagObj = {}, ticker = 'ticker_'+index;

            tagObj            = res.data.ticker_tag;
            tickerObj.ticker  = urlObject[ticker];
            tagObj.term_id    = urlObject[termId];
            tagObj.selected   = true;
            tagObj.its_ticker = { ticker: ticker };

            saveTickerTags(tickerObj, tagObj);
        });
    });

    deferred.resolve(tickerTagsContainer);
    return deferred.promise;
}
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529