1

I have been stuck on this as I am not the best with mixing arrays + string matches.

What I would like to do is return the index number within an array based on a partial match from a string. Full use case; check if text exists in a URL based off values within an array - and return the index of array position.

Don't mind JS or jQuery but whichever might be most efficient is fine (or works).

Current attempt:

Example URL = www.site.com/something/car/123

Another Example URL might be www.site.com/something/somethingelse/banana/

(location of snippet to match is not always in the same path location)

var pageURL = location.href;
var urlArray = ['/car/','/boat/','/apple/','/banana/'];
function contains(urlArray, value) {
var i = urlArray.length;
while (i--) { if (urlArray[i].indexOf(pageURL)) console.log(i)} console.log('Not Found');}

alternate Using jQuery (not sure where to use indexOf or another jQuery alternative (.search / .contains)):

urlArray.each(function(){
$.each(this, function(index) { } )   });

Expected output for first URL would be 0, second example URL would be 3.

Help is much appreciated!

3 Answers 3

2

You can also use a forEach() loop on the urlArray to get each word from the array and check if it exist in url or not.

var url = 'www.site.com/car/somethingelse/banana/';
var urlArray = ['/car/', '/boat/', '/apple/', '/banana/'];
urlArray.forEach(function(word){
  //if word exist in url
  var wordIndex = url.indexOf(word);
  if(wordIndex !== -1){
    console.log(wordIndex);
  }
});

NOTE includes() do not work in IE browser and older versions thus to make it work on all browsers the recommended way is to avoid arrow functions with includes() and instead use plain function with indexOf()

To return the array index:

var url = 'www.site.com/car/somethingelse/banana/';
var urlArray = ['/car/', '/boat/', '/apple/', '/banana/'];
urlArray.forEach(function(word, index){
  //if word exist in url
  if(url.indexOf(word) !== -1){
    console.log(index);
  }
});

Sign up to request clarification or add additional context in comments.

2 Comments

I see this returns the index of the location within the URL if matched, what do I need to return the location within the urlArray (eg. '3' from above example)?
Neat! Thanks for that, works great and I feel it is much easier to manage. Forgive my ignorance but I would just like to confirm as what I was struggling with, is where the word parameter came in to the function (as it wasn't referenced anywhere else)...but it is set from the value brought through the urlArray iterated by forEach. Is that correct?
2

You can iterate over the array with findIndex() to get the index if the includes() the string.

This will go through the urlArray and return the index of the first match (and -1 if a match isn't found).

let URL1 = "www.site.com/something/car/123"
let URL2 = "www.site.com/something/somethingelse/banana/"

let urlArray = ['/car/','/boat/','/apple/','/banana/'];

let index1 = urlArray.findIndex(str => URL1.includes(str))
let index2 = urlArray.findIndex(str => URL2.includes(str))

console.log(index1, index2)

1 Comment

I'm in mobile, so I can't test for myself right now, but what's returned in the event of URL3 = "www.site.com/rumouredDevices/apple/car/index.html"?
0
for (var i = 0; i < urlArray.length; i++) {
 if(pageURL .indexOf(urlArray[i])>-1){
   console.log(pageURL.indexOf(urlArray[i])));
 } 
}

2 Comments

Could you please extend your answer with some comments about why this could be a solution?
Thanks, this actually got me to where I needed once updated. console.log(urlArray.indexOf(urlArray[i])); Is this the best way to reference the index of the array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.