medev21 medev21 - 20 days ago 5
Javascript Question

JavaScript - how to grab unique urls from array based on unique #?

I'm trying to grab unique URLs from this array.

var urls = [
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]


From the code, the fifth array are
['791','246','796','051', '056']
, so the output is like this:

[
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]


From another answer, I was able to grab just the unique fifth codes in an array:

var unique = ['791','246','796', '051','056']


Then I tried to do this in order to grab unique URLs and push it to an empty array, but it's not working out.

var uniqueUrls = []
for(i=0; i < unique.length; i++){
for(j=0; j< urls.length; j++){
if(urls[j].indexOf(unique[i])){
uniqueUrls.push(urls[j]);
break;
}
}
}


Can anybody help me with this? Am I doing something wrong in the for loop? Or is there a better way to grab unique URLs from the URL arrays without using the double for loop?

update

Forgot to mention the other digit codes are not always 'zeros', they can be any random number but I'm still interested in grabbing unique urls based on the fifth code.

Answer

You can use ES6 Set with array spread to get unique values:

const getUnique = (urls) => [...new Set(urls.map((url) => url.match(/\/banner\?l_id=(?:\w+-){4}(\w+)/)[1]))];

var urls = [
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
];
  
console.log(getUnique(urls));

If you can't use ES6, use reduce with a hash object to check for uniqueness:

function getUnique(urls) {
  var hash = {};
  
  return urls.reduce(function(arr, url)  {
    var id = url.match(/\/banner\?l_id=(?:\w+-){4}(\w+)/)[1];
    
    if(!hash[id]) {
      arr.push(id);
      
      hash[id] = true;
    }
    
    return arr;
  }, []);
}

var urls = [
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
  "http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
];
  
console.log(getUnique(urls));