【问题标题】:Comparison between 2 strings in javascript is not workingjavascript中2个字符串之间的比较不起作用
【发布时间】:2013-11-15 18:47:20
【问题描述】:

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

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"))

当我执行此代码时,它会在日志中打印所有比较,然后返回“未定义”。我期望的是在比较成功时停止迭代,并返回 0,这是名称为“卧室”的元素的位置。

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

【问题讨论】:

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

标签: javascript jquery arrays string comparison


【解决方案1】:

你需要从你的函数中返回值。

试试

find_index = function(list, str){
    var idx;
        list.each(function(index, value){
            console.log("Comparing "+value.Name+" with "+str);
            if(value.Name === str){
                idx = index;
                return false; //Exit out of the loop after the match
            }
        });
    return idx;
 };

.each 循环中的返回值用作布尔值以退出或继续循环。

另外你不需要创建一个 jquery 对象,你可以像这样在数组上使用$.each

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"));

【讨论】:

    【解决方案2】:

    Incase,字符串比较失败尝试使用 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"));
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-17
        相关资源
        最近更新 更多