2017-10-06 38 views
-1

我有以下打字稿功能:为什么在这个函数的末尾没有这个数组?

setSelectedTagsFilters(tags) 
{ 
    //init return var 
    var selectedTags = []; 

    //set selected tags filters 
    if (tags && tags.length > 0) 
    { 
     //required for parent context from for loop 
     let that = this; 

     //for each tag qs param 
     for (let tag of tags) 
     { 
      //lookup matches 
      var matches; 
      if (isNumeric(tag)) matches = that.vm.AllTags.filter(x => x.id == tag); 
      else matches = that.vm.AllTags.filter(x => x.text == tag); 

      //if match then add to selectedTags 
      if (matches.length == 0) selectedTags.push(matches[0]); 
     } 
    } 
    //set the tag filters in batch 
    this.setTagFilters(selectedTags); 
} 

我有哪里2场比赛都推到选定的标签的情况。但是,在最后一行代码中,selectedTags是一个空数组。任何想法为什么selectedTags在方法的末尾是空的,即使2个对象被推送到for循环中的selectedTags?

+0

你试过调试吗? –

+0

这条线看起来像一个问题:'if(matches.length == 0)selectedTags.push(matches [0]);' – JohnnyHK

+0

@JohnnyHK那条线看起来有什么问题?我已经证实,在推后的代码行中,selectedTags仍然是空的。但为什么?我该如何解决? – user8570495

回答

0

这条线:

if (matches.length == 0) selectedTags.push(matches[0]); 

是错误的。如果matches.length==0,matches[0]未定义,也没有任何东西被推送到selectedTags

尝试:

if (matches.length > 0) selectedTags.push(matches[0]); 

代替。

+0

是的,我在周五重构和打字时迷路了。谢谢! – user8570495

相关问题