我如何采取一个字符串数组和过滤器呢? [英] How do I take an array of strings and filter them?

查看:117
本文介绍了我如何采取一个字符串数组和过滤器呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery的字符串数组。我有我想要使用的字符串数组过滤关键字的另一个数组。

我的两个数组:

  VAR ARR =新的Array(萨莉工作在塔可钟,汤姆驾驶一辆红色轿车,汤姆是来自俄亥俄州,亚历克斯是来自俄亥俄州);    VAR关键字=新的Array(汤姆,俄亥俄);

我如何过滤使用关键字数组中的jQuery的改编数组?在这种情况下它会过滤掉萨莉工作在塔可钟,并保持休息。

下面是实际code我使用。

  VAR关键字= [];
VAR间隔=;
VAR指针='';
变种涡旋=的document.getElementById(tail_print);$(#filter_button)。点击(
功能(){
    变种ID = $(#filter_box)VAL();
    如果(ID ==--Text--|| ID ==){
        警报(搜索前,请输入文字);
    }其他{
        keywords.push(ID);
        $(#关键字行)追加(< TD>< IMG SRC = \\图像/ delete.png \\类= \\delete_filter \\/>中+编号+< / TD&GT ;);
    }
}
);$(。delete_filter)。点击(
功能(){
   ($(本))删除();
}
);功能startTail(){
clearInterval(间隔);
间隔=的setInterval(
功能(){
    $ .getJSON(?ajax.php功能=尾放;指针=+指针+&放大器;非缓存=+新的Date()
        功能(数据){
            指针= data.pointer;
            $(#tail_print)追加(data.log);
            scroll.scrollTop = scroll.scrollHeight;
        });
},1000);
}

此的整个目的是允许用户过滤日志结果。因此,用户执行一个启动行动 startTail() $。的getJSON()检索内置JSON对象由PHP函数并打印结果。完美的作品。现在我想给用户过滤传入的尾矿项目的选项。用户点击一个按钮,过滤器和jQuery需要过滤器文本,并将其添加到关键字阵列则 data.log 从JSON对象是使用关键字数组,然后追加到筛过滤。

我也有工作不删除过滤功能。也许有人可以帮我这一点。


解决方案

  $。gr​​ep的(ARR,$ .proxy(/./。测试中,新的RegExp(keywords.join( |))));

没有jQuery的:

(/./ test.bind(新的RegExp(keywords.join(|))))

  arr.filter;

I have an array of strings in jQuery. I have another array of keywords that I want to use to filter the string array.

My two arrays:

    var arr = new Array("Sally works at Taco Bell", "Tom drives a red car", "Tom is from Ohio", "Alex is from Ohio");

    var keywords = new Array("Tom", "Ohio");

How can I filter the arr array using the keywords array in jQuery? In this situation it would filter out "Sally works at Taco Bell" and keep the rest.

Below is the actual code I am using.

var keywords= [];
var interval = "";
var pointer = '';
var scroll = document.getElementById("tail_print");

$("#filter_button").click(
function(){
    var id = $("#filter_box").val(); 
    if(id == "--Text--" || id == ""){
        alert("Please enter text before searching.");
    }else{
        keywords.push(id);
        $("#keywords-row").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> " + id + "</td>");
    }
}
);

$(".delete_filter").click(
function(){
   ($(this)).remove(); 
}
);

function startTail(){
clearInterval(interval);
interval = setInterval(
function(){
    $.getJSON("ajax.php?function=tail&pointer=" + pointer + "&nocache=" + new Date(),
        function(data){
            pointer = data.pointer;
            $("#tail_print").append(data.log);
            scroll.scrollTop = scroll.scrollHeight;
        });
}, 1000);
}

The whole purpose of this is to allow the user to filter log results. So the user performs an action that starts startTail() and $.getJSON() retrieves a JSON object that is built by a PHP function and prints the results. Works flawlessly. Now I want to give the user the option to filter the incoming tailing items. The user clicks a filter button and jQuery takes the filter text and adds it to the keywords array then the data.log from the JSON object is filtered using the keywords array and then appended to the screen.

I also have a delete filter function that isn't working. Maybe someone can help me with that.

解决方案

$.grep( arr, $.proxy(/./.test, new RegExp(keywords.join("|"))));

without jQuery:

arr.filter(/./.test.bind(new RegExp(keywords.join("|"))));

这篇关于我如何采取一个字符串数组和过滤器呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆