首先,我检查了关于 SO 的类似主题,例如:Comparing similar strings in jquery 但它们没有帮助。
这是我的代码:
    //jQuery functions
    (function($)
        {
            arrayToString = function(array)
            {
                var s = "";
                var a = [];
                $.each(array, function(i, el){
                    if($.inArray(el, a) === -1)
                        {
                            a.push(el);
                            if (s!="")
                                {
                                    s += "@~@~@";
                                }
                            s += el;
                        }
                });
                return s;
            };
        })(jQuery);
    (function($)
        {
            intersectionOfArrays = function(a,b)
            {
                var array = [];
                $.each(a, function(i, el){
                    if($.inArray(el, array) === -1 && $.inArray(el, b) != -1) array.push(el);
                });
                return array;
            };
        })(jQuery);    
var intersection = arrayToString(intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@")));
alert("intersection = '"+intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@"))+"'");
alert("selectionOf(produitcartesien) = '"+selectionOf("produitcartesien")+"'");
alert("They are different: '"+intersection!=selectionOf("produitcartesien")+"'");
尽管警报有时会显示相同的字符串,但使用!=和!==的比较总是返回true !?
我尝试使用其他一些我不记得的东西,但没有奏效。如何修改上述代码以获得正确答案?
先感谢您。