2015-10-23 96 views
0

使用兩個數組,我想在myArray []中找到patternArray []項。 如果有匹配,我想將myArray中的匹配添加到foundArray。如何使用Google腳本搜索另一個陣列中的模式陣列?

我現在的代碼給了我下面的錯誤;

TypeError: Cannot call method "search" of undefined. (line

任何人都可以告訴我如何解決上述錯誤?此外,此解決方案在myArray中找到多個匹配? (例如,如果 「紅蘋果」(從patternArray)發現更多然後在myarray的一次,我想既增加foundArray。)

function findpattern() 
{ 

    var foundArray=[]; 
    var NotfoundArray=[]; 

    var foundStringCounter=0; 
    var NotfoundStringCounter=0; 

var myArray=["red apple http://awebsite.com/1.jpg","green apple http://1awebsite.com/2.jpg","red apple http://1awebsite.com/3.jpg"]; 

var patternArray= ["green apple","red apple","mango","orange tree","cherry tree","banana store","grape tree"]; 


    var patternArraySize = patternArray.length; 

    for (var i = 0; i < patternArraySize; i++) 
    { 
    var result = myArray[i].search(patternArray[i]); 
    //var result = myArray[i].indexOf(patternArray[i]); 

    if (result == -1) { 

    NotfoundArray.push(myArray[i]); 
    NotfoundStringCounter++; 

    } else { 

    foundArray.push(myArray[i]); 
    foundStringCounter++; 

    } 
    result=""; 
    } 



    Logger.log(foundStringCounter); 
    Logger.log(foundArray); 

    Logger.log(NotfoundStringCounter); 
    Logger.log(NotfoundArray); 
} 

回答

1

此代碼:

function findpattern() { 

    var foundArray=[], 
     NotfoundArray=[], 
     foundStringCounter=0, 
     NotfoundStringCounter=0; 

    var myArray=["red apple http://awebsite.com/1.jpg", 
       "green apple http://1awebsite.com/2.jpg", 
       "red apple http://1awebsite.com/3.jpg", 
       "rotten apple http://rottenApple.com"]; 

    var patternArray= ["green apple","red apple","mango","orange tree","cherry tree","banana store","grape tree"]; 
    var patternArraySize = patternArray.length; 

    Logger.log('patternArraySize: ' + patternArraySize); 

    var i=0, 
     j=0, 
     thisUrl="", 
     thisPattern="", 
     isMatched=0, 
     outerArray = []; 

    for (i=0; i < myArray.length; i++) { 

    thisUrl = myArray[i]; 
    foundStringCounter = 0; //Reset 
    temporaryArray = []; //Reset 

    for (j=0; j<patternArraySize; j+=1) { 
     thisPattern = patternArray[j]; 
     isMatched = thisUrl.indexOf(thisPattern); 

     if (isMatched !== -1) { 
     foundArray.push(thisUrl); 
     foundStringCounter++; 
     }; 

    }; 

    if (foundStringCounter===0) { 
     NotfoundArray.push(thisUrl); 
    }; 

    outerArray.push(foundArray); 
    }; 

    Logger.log('foundArray: ' + foundArray); 
    Logger.log('NotfoundArray: ' + NotfoundArray); 
    Logger.log('outerArray: ' + outerArray); 
}; 

var currentSheet = SpreadsheetApp.getActiveSheet(); 
currentSheet 
    .getRange(currentSheet.getLastRow()+1, 1,outerArray.length,foundArray.length) 
    .setValues(outerArray); 
+0

非常感謝。使用myArray作爲外部FOR循環而不是patternArray有什麼區別? – user1788736

+1

它可能可以以任何方式完成,但在外部循環中使用'myArray'是我更容易理解的方式。 'myArray'包含要找到並返回的元素。你不是試圖從'patternArray'中提取任何東西。另外,這樣,如果有兩個匹配,它們就是從外部循環的「同一個」迭代中找到的。如果反過來這樣做,多次匹配將來自外部循環的不同迭代。這對我來說似乎有點困惑。 –

+0

好的解釋。因此,對於查找模式,我們應該使用indexof而不是搜索和搜索進行精確匹配? – user1788736