javascript - Find index number of array value based on partial string match - Stack Overflow

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

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/something/car/123

Another Example URL might be www.site/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!

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./something/car/123

Another Example URL might be www.site./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!

Share Improve this question edited Sep 24, 2018 at 8:42 user10389796 asked Sep 24, 2018 at 8:12 OMcReyOMcRey 231 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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./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 remended way is to avoid arrow functions with includes() and instead use plain function with indexOf()

To return the array index:

var url = 'www.site./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);
  }
});

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./something/car/123"
let URL2 = "www.site./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)

for (var i = 0; i < urlArray.length; i++) {
 if(pageURL .indexOf(urlArray[i])>-1){
   console.log(pageURL.indexOf(urlArray[i])));
 } 
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349421a4623739.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信