2013-04-22 11 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