0

i'm a beginner in javascript, i apologize in advance for my question if you will find it a little weird or to easy.

Here's my problem for example:

var category = ["orange","lemon","apple","banana"];

var myUrl = "www.website.com/products/15/lemon/image1.png";

When my user will click on an image, i will get a new fruit value for example the string "apple".

var changeCategory = "apple";

What is the more efficient way to check if the word of one of my fruits is in the url and replace it by the new value... or if the new value is actually in myUrl variable to do nothing?

adam
  • 555
  • 1
  • 5
  • 17

4 Answers4

0

Array Check, if element is present

var category = ["Banana", "Orange", "Apple", "Mango"];
var changeCategory  = category.indexOf("Apple");

Output

2 

** If element is not found, return -1.

Parsing URL

var url = window.location.href; to parse current url

// return an array ["www.website.com","products","15","lemon","image1.png"]

var urlArray = url .split( '/' );

var currentFruit = urlArray[3]; //  getting current fruit from url

urlArray[3] = ""; // pass new fruitName in double quotes

// iterate the array to form url now.

var arrayLength = urlArray.length;
Var newUrl;
for (var i = 0; i < arrayLength; i++) {
  if(i < arrayLength-1){
    newUrl = newUrl+ urlArray[i]+ "/";
 }else{
    newUrl = newUrl+ urlArray[i];
    }
}

now you have got the url, just do

window.location.href = newUrl; // redirect page to new url now.
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Thank you ankur for your help and the time you took to help me, if I could I'll vote up your answer too – adam Nov 26 '14 at 02:46
0

Please see the following code. The idea is to take each category element and look for it in myUrl using indexOf(). If indexOf() returns something other than -1, the word has been found and you can replace it with the value of changeCategory by means of replace().

var category = ["orange", "lemon", "apple", "banana"];

var myUrl = "www.website.com/products/15/lemon/image1.png";

var changeCategory = "apple";

for (var i = 0; i < category.length; ++i) {  // Consider each category word.
  if (myUrl.indexOf(category[i]) >= 0) {     // Is it in myUrl?
    myUrl = myUrl.replace(category[i], changeCategory);
    break;                                   // If so, replace it and stop
  }                                          //  looking at the remaining
}                                            //  words.

document.write(myUrl);                       // Test the result.
Michael Laszlo
  • 12,009
  • 2
  • 29
  • 47
0

There are, of course, lots of ways of skinning a cat. You can use a simple regular expression to build the new URL and if it's different to the old, use it. Otherwise, do nothing, e.g.

var oldURL = 'www.website.com/products/15/lemon/image1.png';
var categories = /orange|lemon|apple|banana/;
var newCategory = 'apple'
var newURL = oldURL.replace(categories, newCategory);

if (newURL != oldURL) {
  // go to newURL
}

This will only create a new URL if one of the fruits in categories is in the URL and the replacement fruit is different to the one that's already there.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Even if I use your way in the end... I chose to validate the answer of michael laszlo because it is the one that answer to my original question with the categories in the array. Thanks again Rob. – adam Nov 26 '14 at 15:34
0

You could make category a regular expression.

var changeUrlCategory = function( url, newCat ) {
    var cats = /(orange|lemon|apple|banana)/i;
    return url.replace( cats, newCat );
};

var newUrl = changeUrlCategory( existingUrl, 'grape');
1nfiniti
  • 2,032
  • 14
  • 19