Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是JavascriptClosure用例吗?_Javascript_Arrays_Closures - Fatal编程技术网

这是JavascriptClosure用例吗?

这是JavascriptClosure用例吗?,javascript,arrays,closures,Javascript,Arrays,Closures,我可能有一个初学者Javascript问题: 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.pu

我可能有一个初学者Javascript问题:

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");
var国家=[
“孟加拉国”、“德国”、“巴基斯坦”];
功能测试存在性(arr,输入){
对于(变量i=0;i
我所期望的是:当我再次为'UK'调用函数时,它显示我“已经存在”;但事实并非如此。我不想玩“原型”或定义自己的原型。我只需要一行解决方案

我的代码中有一个用例,我必须在数组中插入一个新值,在接下来的循环中我必须检查该值;但我最终插入了一个现有的值

为什么我要插入现有值,为什么该检查
(arr[I]!=input)
失败

请解释一下,为什么上面的代码没有按预期工作

试试:

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

请注意,
Array.indexOf
在较旧的浏览器中不可用,因此您可以使用polyfill(或保持当前循环)。以下是MDN文档,其中包括polyfill:

试试:

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


请注意,
Array.indexOf
在较旧的浏览器中不可用,因此您可以使用polyfill(或保持当前循环)。这里是它的MDN文档,其中包括一个polyfill:

您需要搜索整个数组,然后才能确定它不存在

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");
}
功能测试存在性(arr,输入){
对于(变量i=0;i


我可能会将您的函数命名为
addUnique
或其他什么,而不是
testexistence

您需要搜索整个数组,然后才能确定它不存在

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");
}
功能测试存在性(arr,输入){
对于(变量i=0;i


我可能会将您的函数命名为
addUnique
或其他什么,而不是
testexistence

您需要尝试类似的方法

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");
var国家=[“伦敦”、“德国”、“法国”];
功能测试存在性(arr,输入){
var isExists=false;
对于(变量i=0;i
您需要尝试类似的方法

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");
var国家=[“伦敦”、“德国”、“法国”];
功能测试存在性(arr,输入){
var isExists=false;
对于(变量i=0;i
你可以用这个代替你的:

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);
}
功能测试存在性(arr,输入){
对于(变量i=0;i
你可以用这个代替你的:

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);
}
功能测试存在性(arr,输入){
对于(变量i=0;i
首先,它绝不是一个闭包

不管怎么说,这是对

我们使用了一些东西:

  • Array.indexOf
    在数组中搜索所传递内容的第一个匹配项,如果存在,则返回一个零基值;如果不存在,则返回一个
    -1
  • ~
    是这里的一个特例,我们在这里测试
    -1
    。值
    ~x
    等于
    -(x+1)
    ,这使得
    -1
    A
    0
    (falsy)和所有其他非零(truthy)。添加
    在混合中使
    -1
    成为一个真实的值,而其他值则是虚假的
  • &&
    对其两侧进行求值。如果左侧为“truthy”,则对右侧进行求值,否则不会。它也被称为“警卫操作员”

首先,它绝不是一个终结

不管怎么说,这是对

我们使用了一些东西:

  • Array.indexOf
    在数组中搜索所传递内容的第一个匹配项,如果存在,则返回一个零基值;如果不存在,则返回一个
    -1
  • ~
    是这里的一个特例,我们在这里测试
    -1
    。值
    ~x
    等于
    -(x+1)
    ,这使得
    -1
    A
    0
    (falsy)和所有其他非零(truthy)。添加
    在混合中使
    -1
    成为一个真实的值,而其他值则是虚假的
  • &&
    对其两侧进行求值。如果左边是“真实的”,t