首先,这是urlObject
的样子:
{
term_id_1: "2155"
term_id_2: "2894"
ticker_1: "SPY"
ticker_2: "SPY"
}
我最多可以有3个条款和代码:
ticker_1
,ticker_2
,ticker_3
使用匹配的标签:
term_id_1
,term_id_2
,term_id_3
这是我当前的函数,它检查是否存在自动收报机和标签组合,然后调用另一个函数:
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);
}
}
这感觉很容易简化,你会怎么做?
答案 0 :(得分:1)
没有真正测试过多,但这应该有效,这样你就不必对id进行硬编码......
s = 'Join us!https://t.co/Fe0oTahdom'
idx = s.index('https')
parts = [s[0:idx], s[idx:]]
答案 1 :(得分:0)
为ticker
和term_id
创建一个函数并调用它三次。
如果您喜欢冒险,请写一个从1到3的循环looks up the properties dynamically。
答案 2 :(得分:0)
创建一个接受测试条件并调用它的函数。
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])
}
或者如果ticker_1
,term_id_1
总是处于这种形式,那么
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);
}
}
}
答案 3 :(得分:0)
以下是已经提到过的函数的实现方式:
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);
}
答案 4 :(得分:0)
注意,我在这里发布了这个答案,因为这是我最终选择的解决方案。
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;
}