2013-04-22 14 views
0

我也許已經初學者的Javascript問題:這是JavascriptClosure用例嗎?

var countries = [ 
    "Bangladesh", "Germany", "Pakistan"]; 


function testexistence(arr, input) { 

    for (var i = 0; i < arr.length; i++) { 
     if (arr[i] != input) { 
      alert("not exist"); 
      arr.push(input); 
      break; 
     } else { 
      alert("already exist "); 
     } 
    } 

} 

testexistence(countries, "UK"); 
testexistence(countries, "Pakistan"); 
testexistence(countries, "UK"); 

我想到的是:當我再次調用該函數的「英國」它爲我「已經存在」;但這並未發生。我不想玩「原型」或定義我自己的一個。我只需要一個線路解決方案。

我在我的代碼中有一個用例,我必須在數組中插入一個新值並在下一個循環中檢查該值;但我最後插入一個現有的值...

爲什麼我結束了插入現有值,爲什麼這個檢查(arr[i] != input)失敗?

還請解釋一下,爲什麼根據需要將上面的代碼是不工作

+1

您應該每次都推送輸入,而不是「英國」。它不工作? – bfavaretto 2013-04-22 16:18:07

+0

'testexistence'不是函數的好名字。 'pushIfUnique'或'pushUnique'如何? – 2013-04-23 00:18:17

回答

2

嘗試:

function testexistence(arr, input) { 
    if (!~arr.indexOf(input)) { 
     arr.push(input); 
    } 
} 

DEMO:http://jsfiddle.net/L9NhU/

注意Array.indexOf不可用在較老的瀏覽器中,所以你可以使用polyfill(或保持當前的循環)。以下是MDN文檔,其中包含一個polyfill:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

+1

看起來像他的代碼想要添加一些測試時不存在的東西。需要修改:D – Joseph 2013-04-22 16:18:17

+0

@JosephtheDreamer廢話,你是對的。 – Ian 2013-04-22 16:19:07

+0

爲什麼你使用〜因爲你的「是排列?」測試?它比'return(arr.indexOf(input)!= -1)'更有效率嗎? – andytuba 2013-04-22 16:19:39

3

您需要先搜索整個數組,然後才能確定它不存在。

function testexistence(arr, input) { 
    for (var i = 0; i < arr.length; i++) { 
     if (arr[i] === input) { 
      alert("already exists"); 
      return; // halt the search by returning 
     } 
    } 

    // If we're here, we never returned inside the loop, so it wasn't found. 
    arr.push(input); 
    alert("did not exist, now it does"); 
} 

相反的testexistence,我可能會命名功能addUnique什麼的。

+0

由於您提供瞭解決方案,因此您應該推送'input',而不是'UK'。 – cfs 2013-04-22 16:22:47

+0

@cfs:嘿,甚至沒有注意到這一點。只需複製並粘貼代碼。謝謝! – 2013-04-22 16:23:26

0

你需要嘗試這樣

var countries = ["london", "germany", "france"]; 


function testexistence(arr, input) { 
    var isExists = false; 

    for (var i = 0; i < arr.length; i++) { 
     if (arr[i] == input) { 
      isExists = true; 
     }   
    } 

    if(!isExists) 
    { 
     alert("Not Exists"); 
     arr.push(input); 
    } 
    else 
    { 
     alert("Exists"); 
    } 
} 

testexistence(countries, "UK"); 
testexistence(countries, "london"); 
testexistence(countries, "UK"); 
0

你可以改用你這樣的一些感受:

function testexistence(arr, input) { 

    for (var i = 0; i < arr.length; i++) { 
     if (arr[i] == input) { 
      alert("already exist "); 
      return; 
     } 
    } 

    //if the if part would not work, you pass to here 
    alert("not exist"); 
    arr.push(item); 
} 
1

其一,它不以任何方式封閉

不管怎麼說,這裏的the one-liner you wantedIan's answer

function testexistence(arr, input) { 
    (!~arr.indexOf(input)) && arr.push(input); 
} 

我們用幾件事情的修改:

  • Array.indexOf搜索的數組,你通過什麼樣的第一場比賽,並返回一個零如果存在,則返回值;如果不存在,則返回-1
  • !~這裏是一個特例,我們在這裏測試-1。值~x等於-(x+1),這使得-1 a 0(虛假)和所有其他非零(真實)。將!添加到混合使得-1真實價值和其他麻煩。
  • &&評估其兩側。如果左邊是「truthy」,那麼評估右邊,否則它不會。它也被稱爲「警衛運營商」
+1

你應該避免以這種方式使用'&&'。正如JavaScript創建者Brendan Eich在他的博客中指出的,[這是一種*濫用*](http://brendaneich.com/2012/04/the-infernal-semicolon/)。最好使用實際的if語句代替。你也不會使用更多的字符;只需將'&&'變成'if'並將其放在條件的另一側。 – Sampson 2013-04-23 01:14:15