Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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
Javascript 搜索数组返回部分匹配项_Javascript_Arrays_Search_Mootools - Fatal编程技术网

Javascript 搜索数组返回部分匹配项

Javascript 搜索数组返回部分匹配项,javascript,arrays,search,mootools,Javascript,Arrays,Search,Mootools,我需要在关联数组的值中搜索字符串,但只搜索字符串示例的开头: var stack = ['aba', 'abcd', 'ab', 'da', 'da']; 在堆栈上搜索值a将返回['abc',abcd','ab'],而搜索b将只返回b,而搜索'd'将返回[da','da']…有什么方法可以做到这一点吗 我试着像自动完成选择框那样做,但它是自定义的,所以我需要修改文本事件并搜索我的项目数组,以便在用户键入时获得第一个匹配项的索引。array.prototype.startWith=functio

我需要在关联数组的值中搜索字符串,但只搜索字符串示例的开头:

var stack = ['aba', 'abcd', 'ab', 'da', 'da'];
在堆栈上搜索值
a
将返回
['abc',abcd','ab']
,而搜索
b
将只返回b,而搜索'd'将返回
[da','da']
…有什么方法可以做到这一点吗

我试着像自动完成选择框那样做,但它是自定义的,所以我需要修改文本事件并搜索我的项目数组,以便在用户键入时获得第一个匹配项的索引。

array.prototype.startWith=function(c){
/**
 * Extend the Array object
 * @param candid The string to search for
 * @returns Returns the index of the first match or -1 if not found
*/
Array.prototype.searchFor = function(candid) {
    for (var i=0; i<this.length; i++) {
        if (this[i].indexOf(candid) == 0) {
            return i;
        }
    }
    return -1;
};
var结果=[]; 对于(var i=0,len=this.length;i
Array.prototype.startWith=function(c){
var结果=[];

对于(var i=0,len=this.length;i如果要使用mootools来执行此操作,可以使用mootools中的filter方法:

function search(arr, letter) { 
    var matches = arr.filter(function(str) {
        return str.charAt(0) == letter;
    });

    return (matches.length > 0) ? matches : letter;
}

search(stack, 'd'); //returns ['da', 'da']

如果要使用mootools执行此操作,可以使用mootools中的筛选方法:

function search(arr, letter) { 
    var matches = arr.filter(function(str) {
        return str.charAt(0) == letter;
    });

    return (matches.length > 0) ? matches : letter;
}

search(stack, 'd'); //returns ['da', 'da']

UpDype @ Mrbuubuu,但您可以将其作为原型,并通过String <代码>传递筛选器元素。包含 >更多MOOTooSISE,并为中间匹配,如“CD”,它应该返回结果。

例如,一组品牌,其中一个是
北面
,用户搜索
北面
时应返回匹配的品牌,但不会返回,因为他们错过了

此外,在比较值时,需要确保降低搜索字符串和堆栈数组元素的大小写

下面是一个输入有效的示例:

或者,您在评论中提到,您真正想要得到的只是原始数组中的索引:

(function() {
    Array.implement({
        getIndexes: function(what) {
            var indexes = [];
            this.each(function(el, index) {
                if (el.charAt(0) == what)
                    indexes.push(index);
            });
            return indexes;
        }
    });
})();


console.log(['aba', 'abcd', 'ab', 'da', 'da'].getIndexes("d")); 
// [3,4]

虽然这个数组不返回数组,但是它会破坏链接,因此它不应该是一个数组的原型,而只是一个函数。

UpDype @ Mrbuubuu,但是你可以把它作为一个原型,并通过字符串<代码>传递过滤元素。ld返回结果

例如,一组品牌,其中一个是
北面
,用户搜索
北面
时应返回匹配的品牌,但不会返回,因为他们错过了

此外,在比较值时,需要确保降低搜索字符串和堆栈数组元素的大小写

下面是一个输入有效的示例:

或者,您在评论中提到,您真正想要得到的只是原始数组中的索引:

(function() {
    Array.implement({
        getIndexes: function(what) {
            var indexes = [];
            this.each(function(el, index) {
                if (el.charAt(0) == what)
                    indexes.push(index);
            });
            return indexes;
        }
    });
})();


console.log(['aba', 'abcd', 'ab', 'da', 'da'].getIndexes("d")); 
// [3,4]

虽然这不会返回数组,但会破坏链接,因此它不应该是数组的原型,而只是一个函数。

实现这一点的最简单的普通javascript是

var stack = ['aba', 'abcd', 'ab', 'da', 'da', undefined, , false, null, 0];
var prefixTextToFind = "a"; //b, c or d

var matches = stack.filter(function(stackValue){
  //get rid of all falsely objects
  if(stackValue) {
    return (stackValue.substring(0, prefixTextToFind.length) === prefixTextToFind);
  }
}); //["aba", "abcd", "ab"]

实现这一点的最简单的香草javascript是

var stack = ['aba', 'abcd', 'ab', 'da', 'da', undefined, , false, null, 0];
var prefixTextToFind = "a"; //b, c or d

var matches = stack.filter(function(stackValue){
  //get rid of all falsely objects
  if(stackValue) {
    return (stackValue.substring(0, prefixTextToFind.length) === prefixTextToFind);
  }
}); //["aba", "abcd", "ab"]

实际上,我需要返回的是最接近的matchmootools的索引,这也很好,没有关联数组。您不能通过
堆栈['aba']
访问“aba”。在“ab”之后还有一个未封装的字符串。实际上,我需要返回的是最接近的matchmootools的索引,也没有关联数组。您不能通过
堆栈['aba']
访问“aba”。在“ab”之后还有一个未封装的字符串。在新浏览器中,筛选器是内置的数组方法。在新浏览器中,筛选器是内置的数组方法。如果数组包含:
here-i-come
,我想查看Url:
http://mypage.com/noway/here-i-come
具有数组中的值?如果数组包含:
here-i-come
并且我想查看Url是否为:http://mypage.com/noway/here-i-come
具有数组中的值?什么是
数组。实现
?我以前从未见过,也找不到任何关于它的文档。这是一种特定于mootools的方法来扩展array.prototype-就像说
array.prototype.getIndexes=()=>…
啊,明白了。谢谢你的后续行动。什么是
数组。实现
?我以前从未见过,也找不到任何关于它的文档。这是一种特定于mootools的方法来扩展Array.prototype-就像说
Array.prototype.getIndexes=>…
啊,明白了。谢谢你的后续行动。