如何在Javascript中检查几个数组的输入单词

时间:2015-05-08 12:06:52

标签: javascript jquery arrays ajax

我需要针对3个对象测试输入到输入中的所有单词,并确定它们属于哪个数组,以便我可以将URL输出到API。 我想用Javascript / jQuery来实现这个目标。

例如,如果输入包含以下字词:keyword1 keyword2 keyword3 keyword5

所有关键字条目都将从自动填充插件中添加。

然后我需要针对3个阵列测试它们。

var array1 = ["keyword2", "keyword6"];
var array2 = ["keyword3", "keyword4"];
var array3 = ["keyword1", "keyword5"];

我需要确定它们来自哪个数组,以便输出URL并将值添加到URL中的特定键。

示例:

domain.com/api?array1= [insert keyword(s)]& array2 = [insert keyword(s)]& array3 = [insert keyword(s)]

关键字需要作为数组发送,并且必须用短划线替换空格。

我正在使用jQuery通过生成的URL执行GET请求。

2 个答案:

答案 0 :(得分:0)

您可以通过创建数组数组来缩短代码,但这可以正常工作

var input = "keyword1 keyword2 keyword3 keyword5".split(" ");
var array1 = ["keyword2", "keyword6"];
var array2 = ["keyword3", "keyword4"];
var array3 = ["keyword1", "keyword5"];
var arr1=[],arr2=[],arr3=[];

$.each(input,function(_,keyword) {
  if ($.inArray(keyword,array1) !=-1) arr1.push(keyword);
  if ($.inArray(keyword,array2) !=-1) arr2.push(keyword);
  if ($.inArray(keyword,array3) !=-1) arr3.push(keyword);
});
var url = "domain.com/api/?",keywords="";
if (arr1.length>0) keywords += "&array1="+arr1.join(",");
if (arr2.length>0) keywords += "&array2="+arr2.join(",");
if (arr3.length>0) keywords += "&array3="+arr3.join(",");
if (keywords.length>0) url += keywords.substring(1).replace(/ /g,"-");
console.log(url)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

答案 1 :(得分:0)

这是一个香草JS版本:

var words = 'keyword1 keyword2 keyword3 keyword5';

// first create an object that contains your arrays
var dict = {
    array1: ["keyword2", "keyword6"],
    array2: ["keyword3", "keyword4"],
    array3: ["keyword1", "keyword5"]
}

// start building up a new object that mirrors the existing one
// but that only contains those keywords that are in the input string
function buildURLObj(dict, words) {
    var out = {};

    // split the keywords string into an array
    words = words.split(' ');

    // loop over the object
    for (var p in dict) {
        out[p] = [];

        // loop over the array of keywords
        for (var i = 0, l = words.length; i < l; i++) {

            // if the keyword in the array, push it to the
            // temporary object
            if (dict[p].indexOf(words[i]) > -1) {
                out[p].push(words[i]);
            }
        }
    }

    // return the completed URL using createURL
    return createURL(out);
}

// create a URL from the new object
function createURL(arr) {
    var url = [];
    for (var p in arr) {

        // if the array is not empty, don't add it to the completed URL
        // otherwise start building up the URL string
        if [arr[p].length) {
            var subURL = [];
            subURL.push(p);
            subURL.push('[' + arr[p].join('-') + ']');
            url.push(subURL.join('='));
        }
    }

    // return the completed URL
    return url.join('&');
}

// "array1=[keyword2]&array2=[keyword3]&array3=[keyword1-keyword5]"
buildURL(dict, words);

DEMO