JavaScript How to check an input words against several arrays in Javascript

I need to test all the words entered into an input against 3 objects and determine which array they belong to so I can output a URL to an API. I want to achieve this with Javascript/jQuery.

For example if the input had these words: keyword1 keyword2 keyword3 keyword5

All keyword entries will be added from a autocomplete plugin.

I then need to test them against 3 arrays.

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

I need to determine what array they came from so I can output a URL and add the values to specific keys in a URL.

Example:

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

The keywords need to be sent as an array and must have spaces replaced with dashes.

I am using jQuery to perform a GET request with the URL generated.

Answer:1

You can make the code shorter by creating an array of arrays but this works

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>
Answer:2

I'm having a bit of trouble trying to loop out multiple markers onto a map using information stored in an array. The code creates the map no problem, but it's not displaying the markers I'm trying ...

I'm having a bit of trouble trying to loop out multiple markers onto a map using information stored in an array. The code creates the map no problem, but it's not displaying the markers I'm trying ...

  1. google maps creating a route
  2. google maps creating a map
  3. google maps creating custom markers
  4. google maps creating markers
  5. google maps creating custom map
  6. creating google maps with pins
  7. creating google maps api key
  8. creating google maps from postcode data
  9. creating google maps directions

I'm trying to find a way to get a url from an iframe and using javascript modify the link and move to a another page. basicly the address in the iframe is something like "http://www.somthing.com/12/...

I'm trying to find a way to get a url from an iframe and using javascript modify the link and move to a another page. basicly the address in the iframe is something like "http://www.somthing.com/12/...

Here's a replication of the problem: I get the timestamp of now via JavaScript as var ts = +new Date // 1368971991090 console.log( new Date(1368971991090) ); // Sun May 19 2013 13:59:51 GMT+0000 (GMT)...

Here's a replication of the problem: I get the timestamp of now via JavaScript as var ts = +new Date // 1368971991090 console.log( new Date(1368971991090) ); // Sun May 19 2013 13:59:51 GMT+0000 (GMT)...

I am using the following coding for the script in external file function DiableLink(elem){ console.log($(elem).attr('type')); if($(elem).hasClass('disabled')) { return false; } ...

I am using the following coding for the script in external file function DiableLink(elem){ console.log($(elem).attr('type')); if($(elem).hasClass('disabled')) { return false; } ...