javascript中两个字符串之间的比较无效

javascript中两个字符串之间的比较无效,javascript,jquery,arrays,string,comparison,Javascript,Jquery,Arrays,String,Comparison,我编写这个代码示例是为了知道在“Name”中具有特定值的元素的索引位置。我的变量数据包含这些元素的列表 var data = {"Attributes":[ {"Name":"bedrooms","Value":"4"}, {"Name":"bathrooms","Value":"2"}, {"Name":"property_type","Value":"House"}, {"Name":"rateable_value","Value":"$780,000"},

我编写这个代码示例是为了知道在“Name”中具有特定值的元素的索引位置。我的变量数据包含这些元素的列表

var data = {"Attributes":[
    {"Name":"bedrooms","Value":"4"},
    {"Name":"bathrooms","Value":"2"},
    {"Name":"property_type","Value":"House"},
    {"Name":"rateable_value","Value":"$780,000"},
    {"Name":"price","Value":"Price by negotiation"},
    {"Name":"location","Value":"13"},
    {"Name":"district","Value":"Queenstown-Lakes"},
    {"Name":"suburb","Value":"Lower Shotover"},
    {"Name":"region","Value":"Otago"},
    {"Name":"floor_area","Value":"254m²"},
    {"Name":"land_area","Value":"1690m²"},
    {"Name":"property_id","Value":"CBM959"},
    {"Name":"in_the_area","Value":"playground"},
    {"Name":"parking","Value":"Large double garage"}
]}

find_index = function(list, str){
        list.each(function(index, value){
            console.log("Comparing "+value.Name+" with "+str);
            if(value.Name == str){
                return index;
            }
        });
    };

console.log(find_index($(data.Attributes), "bedrooms"))
当我执行这段代码时,它会在日志中打印所有比较结果,然后返回“undefined”。我所期望的是在比较成功时停止迭代,并返回0,这是名为“bedrooms”的元素的位置


这里发生了什么?我如何解决它呢?

您需要从函数返回值

试一试

中的返回值。每个
循环都用作布尔值以退出或继续循环

另外,您不需要创建jquery对象,只需使用
$即可。数组上的每个
,如下所示:

find_index = function(list, str){
        var idx;
            $.each(list, function(index, value){
                 if(value.Name === str){
                    idx = index;
                    return false; //Exit out of the loop after the match
                }
            });
        return idx;
     };
console.log(find_index(data.Attributes, "bedrooms"));
事实上,您可以更好地简化此操作,特别是如果您希望获得多个匹配项。但是没有办法突破地图:

find_index = function(list, str){
  return $.map(list, function(val, idx){
       if(val.Name === str) return idx;
   })[0]; //for multiple matches remove 0
};

console.log(find_index(data.Attributes, "district"));

您需要从函数返回值

试一试

中的返回值。每个
循环都用作布尔值以退出或继续循环

另外,您不需要创建jquery对象,只需使用
$即可。数组上的每个
,如下所示:

find_index = function(list, str){
        var idx;
            $.each(list, function(index, value){
                 if(value.Name === str){
                    idx = index;
                    return false; //Exit out of the loop after the match
                }
            });
        return idx;
     };
console.log(find_index(data.Attributes, "bedrooms"));
事实上,您可以更好地简化此操作,特别是如果您希望获得多个匹配项。但是没有办法突破地图:

find_index = function(list, str){
  return $.map(list, function(val, idx){
       if(val.Name === str) return idx;
   })[0]; //for multiple matches remove 0
};

console.log(find_index(data.Attributes, "district"));

除非返回false,否则每个函数都不会停止(中断)。您可以将匹配索引分配给一个变量,然后返回false。

除非返回false,否则每个函数都不会停止(中断)。您可以将匹配索引分配给一个变量,然后返回false。

在这种情况下,字符串比较失败,请尝试使用localeCompare;这是为了精确匹配字符串

find_index = function(list, str){
var localIndex;
    list.each(function(index, value){
        console.log("Comparing "+value.Name+" with "+str + index);
        if(str.localeCompare(value.Name) == 0)
        {

            localIndex = index;
            return false;
        }

    });
    return localIndex;
};

console.log(find_index($(data.Attributes), "bedrooms"));

在这种情况下,字符串比较失败,请尝试使用localeCompare;这是为了精确匹配字符串

find_index = function(list, str){
var localIndex;
    list.each(function(index, value){
        console.log("Comparing "+value.Name+" with "+str + index);
        if(str.localeCompare(value.Name) == 0)
        {

            localIndex = index;
            return false;
        }

    });
    return localIndex;
};

console.log(find_index($(data.Attributes), "bedrooms"));

您的
return
语句从内部匿名函数返回,而不是从
find_index
函数返回。您的
return
语句从内部匿名函数返回,而不是从
find_index
函数返回。