user1038065
user1038065

Reputation:

Trying to create a function which extracts a URL from an array. JavaScript

So basically I would like to create a function that when alerted, returns the URL from an array (in this case the array is declared as 'websites'). The function has two parameters 'websites' and 'searchTerm'.

I'm struggling to make the function behave, so that when i type yahoo or google or bing in the searchTerm parameter for the function; I want it to return the corresponding URL.

Any help or support would be greatly appreciated. Sorry if I have not made myself clear in my explanation, if this is the case, let me know and I will try and be clearer in my explanation.

Thanks in advance!

Upvotes: 0

Views: 76

Answers (5)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

Try something more like:

var websites = {google: 'www.google.com', yahoo: 'www.yahoo.com'};
function filterURL(websites,searchTerm) 
{
    return websites[searchTerm] || 'www.defaultsearchwebstirehere.com';
}

** Update following comment **

Build up your websites object like so (where input is your array of key values seperated by pipe characters):

var websites = {};
for (var i = 0; i < input.length; i++) {
  var siteToSearchTerm = input[i].split('|');
  websites[siteToSearchTerm[1]] = siteToSearchTerm[0];

}

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382776

Here is how:

var websites = ["www.google.com|Google" , "www.yahoo.com|Yahoo" , "www.bing.com|Bing"];

function filterURL(websites,searchTerm) 
{
   for (var i = 0; i < websites.length; i++) {
      if (websites[i].split('|')[1] === searchTerm) {
         return websites[i].split('|')[0];
      }
   }
}

Working Example

You can also validate and improve function:

function filterURL(websites,searchTerm) 
{
   if (typeof websites != 'Array' || ! searchTerm) return false;

   for (var i = 0; i < websites.length; i++) {
      if (websites[i].split('|')[1] === searchTerm) {
         return websites[i].split('|')[0];
      }
   }

   return false;
}

Upvotes: 1

kojiro
kojiro

Reputation: 77137

I'm not sure why you want to use an array for this, as what you're really doing fits a key-value pair better; however, here's how I'd do it:

function filterURL(websites, searchTerm) {
    var i = 0,
        parts;
    for (i = 0; i < websites.length; i++) {
        parts = websites[i].split("|");
        if (parts[1].toLowerCase() === searchTerm) {
            return parts[0];
        }
    }
}

But consider if you used a proper JavaScript Object instead:

var websites = {
    Google: "www.google.com",
    Yahoo: "www.yahoo.com",
    Bing: "www.bing.com"
}
// Now it's much simpler:
function filterURL(websites, searchTerm) {
    // key has first letter capitalized…
    return websites[searchTerm.charAt(0).toUpperCase() + searchTerm.slice(1).toLowerCase()];
}

Upvotes: 0

Ben
Ben

Reputation: 10104

You should really be using a hash-table like structure so that you don't have to search through the whole array every time. Something like this:

var websites = {
    "Google": "www.google.com",
    "Yahoo": "www.yahoo.com",
    "Bing": "www.bing.com"
};
function filterURL(websites, searchTerm) {
    if (websites[searchTerm] !== undefined)
        return websites[searchTerm];
    else
        return null;
}

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

Why not just use an object?

var websites = {
    Google: 'www.google.com',
    Yahoo: 'www.yahoo.com'
};

function filterURL(sites, searchTerm) {
    if (sites[searchTerm]) {
        return sites[searchTerm];
    } else {
        // What do you want to do when it can't be found?
    }
}

alert(filterURL(websites, 'Google')); // alerts 'www.google.com'

Upvotes: 0

Related Questions