Home > Enterprise >  JQuery search and compare part of word in array
JQuery search and compare part of word in array

Time:05-04

I'm beginner with JQuery, and I have my first issues ! ;) I want to search particuliar word/number from array in many url.

  • I list URLs in an "aLinks" array.
  • The URLs of aLinks are in the form "../page/article?359&lng=5"
  • I have a second table with reference values to look for (e.g.: ["359","686"])

I tried several methods, but the match is only found with the full URL, and never with only "359" which is one of the values I’m looking for...

I'm supposed it' because isn't a substring (no space in the sentence) ? What I can do, use search with regex? match?

/* 01 - Parse links in aLinks array */

  jQuery(document).ready(function parseLink() {
        
    var aLinks = $('a.link.article').map( function() {
        return $(this).attr('href');
    }).get();
   
/*  02 - Loop and search over array */
  
  var refArray = ['359', '../page/article?686&lng=5'];
  
  $.each(aLinks, function(idx, value) {
        if ($.inArray(value, refArray) !== -1) {
            console.log('MATCH : '   value);

/*
    Only the second index works... because isn't a substring ?
*/

        } else {
            console.log('NOT MATCH: '   value);
        }
    });
});

Thanks ! Best regards, Fiz

EDIT : With the solution of cheesyMan, I just added concatenation with urlStart and urlEnd :


urlStart = '../page/article?';
urlEnd = '&lng='

$.each(aLinks, function(idx, val) {
    let matching = false;
    for (let i = 0; i < compare.length; i  ) {
        if (val.includes(urlStart   compare[i]   urlEnd)) {
          console.log(`${val} MATCHED!`);
          matching = true;
        }
    }

        if (!matching) console.log(`${val} DID NOT MATCH...`)
        })

It's OK, Thanks a lot at all !!

CodePudding user response:

Here I have a script that searches the #SUuL1 input. From the #uList1 and replaces it with a mark tag. Not exactly what you are looking for, but it's something you can fiddle around with. Indeed the part that you are looking for is the RegExp. // search for all ✌️

let searched = document.getElementById("SUuL1").value.trim();
if (searched !== "") {
    let text = document.getElementById("uList1").innerHTML;
    let re = new RegExp(searched  "(?![^<>]*>)","ig"); // search for all
    let newText = text.replace(re, `<mark>${searched}</mark>`);
    document.getElementById("uList1").innerHTML = newText;
}

CodePudding user response:

Inside your $.each() loop you compare each link inside aLinks[] with the values inside refArray[], using strict equality.

So, each value of the first array (aLinks) must be strictly equal to (===) one of the elements of the second array (refArray).

I think you need a nested loop inside $.each (to compare each link against each of the references) and to compare them with includes(). Also substring() could be useful in cases like this.

I've used some fake arrays in this snippet, don't know if they fit to your data.

const compare = [
  '389', '423', '../page/article?915&lng=5', '212', '../page/article?686&lng=3'
]

const links = [
  '../page/article?359&lng=5', '../page/article?389&lng=2', '../page/article?459&lng=4', '../page/article?212&lng=1', '../page/article?915&lng=5'
]


$.each(links, function(idx, val) {
  let matching = false;
  for (let i = 0; i < compare.length; i  ) {
    if (val.includes(compare[i])) {
      console.log(`${val} MATCHED!`);
      matching = true;
    }
  }

  if (!matching) console.log(`${val} DID NOT MATCH...`)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related